java.lang.Object
g2701_2800.s2781_length_of_the_longest_valid_substring.Solution

public class Solution extends Object
2781 - Length of the Longest Valid Substring.<p>Hard</p> <p>You are given a string <code>word</code> and an array of strings <code>forbidden</code>.</p> <p>A string is called <strong>valid</strong> if none of its substrings are present in <code>forbidden</code>.</p> <p>Return <em>the length of the <strong>longest valid substring</strong> of the string</em> <code>word</code>.</p> <p>A <strong>substring</strong> is a contiguous sequence of characters in a string, possibly empty.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> word = &ldquo;cbaaaabc&rdquo;, forbidden = [&ldquo;aaa&rdquo;,&ldquo;cb&rdquo;]</p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation:</strong></p> <p>There are 9 valid substrings in word: &ldquo;c&rdquo;, &ldquo;b&rdquo;, &ldquo;a&rdquo;, &ldquo;ba&rdquo;, &ldquo;aa&rdquo;, &ldquo;bc&rdquo;, &ldquo;baa&rdquo;, &ldquo;aab&rdquo;, &ldquo;ab&rdquo;, &quot;abc&quot;and &ldquo;aabc&rdquo;. The length of the longest valid substring is 4.</p> <p>It can be shown that all other substrings contain either &ldquo;aaa&rdquo; or &ldquo;cb&rdquo; as a substring.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> word = &ldquo;leetcode&rdquo;, forbidden = [&ldquo;de&rdquo;,&ldquo;le&rdquo;,&ldquo;e&rdquo;]</p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation:</strong></p> <p>There are 11 valid substrings in word: &ldquo;l&rdquo;, &ldquo;t&rdquo;, &ldquo;c&rdquo;, &ldquo;o&rdquo;, &ldquo;d&rdquo;, &ldquo;tc&rdquo;, &ldquo;co&rdquo;, &ldquo;od&rdquo;, &ldquo;tco&rdquo;, &ldquo;cod&rdquo;, and &ldquo;tcod&rdquo;. The length of the longest valid substring is 4.</p> <p>It can be shown that all other substrings contain either &ldquo;de&rdquo;, &ldquo;le&rdquo;, or &ldquo;e&rdquo; as a substring.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= word.length <= 10<sup>5</sup></code></li> <li><code>word</code> consists only of lowercase English letters.</li> <li><code>1 <= forbidden.length <= 10<sup>5</sup></code></li> <li><code>1 <= forbidden[i].length <= 10</code></li> <li><code>forbidden[i]</code> consists only of lowercase English letters.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • longestValidSubstring

      public int longestValidSubstring(String word, List<String> forbidden)