Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Length of the Longest Valid Substring

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "2781" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

You are given a string word and an array of strings forbidden. A string is called valid if none of its substrings are present in forbidden. Return the length of the longest valid substring of the string word. A substring is a contiguous sequence of characters in a string, possibly empty. Example 1: Input: word = "cbaaaabc", forbidden = ["aaa","cb"] Output: 4 Explanation: There are 11 valid substrings in word: "c", "b", "a", "ba", "aa", "bc", "baa", "aab", "ab", "abc" and "aabc". The length of the longest valid substring is 4. It can be shown that all other substrings contain either "aaa" or "cb" as a substring. Example 2: Input: word = "leetcode", forbidden = ["de","le","e"] Output: 4 Explanation: There are 11 valid substrings in word: "l", "t", "c", "o", "d", "tc", "co", "od", "tco", "cod", and "tcod". The length of the longest valid substring is 4. It can be shown that all other substrings contain either "de", "le", or "e" as a substring. Constraints: 1 <= word.length <= 105 word consists only of lowercase English letters. 1 <= forbidden.length <= 105 1 <= forbidden[i].length <= 10 forbidden[i] consists only of lowercase English letters.

Explanation

Here's a breakdown of the approach, followed by the Python code:

  • Sliding Window: Use a sliding window to iterate through the word. The window represents a potential valid substring.
  • Check for Forbidden Substrings: For each window, efficiently check if any of the forbidden strings are present as substrings.
  • Maintain Longest Valid Substring: Keep track of the maximum length of valid substrings encountered.

  • Runtime Complexity: O(n * m), where n is the length of the word and m is the total length of all strings in forbidden.

  • Storage Complexity: O(k), where k is the number of forbidden strings and their average length.

Code

    def longest_valid_substring(word: str, forbidden: list[str]) -> int:
    """
    Finds the length of the longest valid substring of the string word.
    A string is called valid if none of its substrings are present in forbidden.
    """
    forbidden_set = set(forbidden)
    max_len = 0
    left = 0
    for right in range(len(word)):
        while True:
            valid = True
            for length in range(1, min(11, right - left + 2)):
                sub = word[right - length + 1: right + 1]
                if sub in forbidden_set:
                    left = right - length + 2
                    valid = False
                    break
            if valid:
                break

        max_len = max(max_len, right - left + 1)

    return max_len

More from this blog

C

Chatmagic blog

2894 posts