Length of the Longest Valid Substring

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
Inputword = "cbaaaabc", forbidden = ["aaa","cb"]
Output4
There are 11 valid substrings in word, and the longest valid substring is "aabc" with length 4.
Example 2
Inputword = "leetcode", forbidden = ["de","le","e"]
Output4
There are 11 valid substrings in word, and the longest valid substring is "tcod" with length 4.

Constraints

  • 1 <= word.length <= 10^5
  • word consists only of lowercase English letters.
  • 1 <= forbidden.length <= 10^5
  • 1 <= forbidden[i].length <= 10
  • forbidden[i] consists only of lowercase English letters.

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate