-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2781.length-of-the-longest-valid-substring.py
81 lines (75 loc) · 2.03 KB
/
2781.length-of-the-longest-valid-substring.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#
# @lc app=leetcode id=2781 lang=python3
#
# [2781] Length of the Longest Valid Substring
#
# https://leetcode.com/problems/length-of-the-longest-valid-substring/description/
#
# algorithms
# Hard (28.38%)
# Likes: 211
# Dislikes: 2
# Total Accepted: 4.3K
# Total Submissions: 15.4K
# Testcase Example: '"cbaaaabc"\n["aaa","cb"]'
#
# 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 9 valid substrings in word: "c", "b", "a", "ba", "aa",
# "bc", "baa", "aab", 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 <= 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.
#
#
#
# @lc code=start
class Solution:
def longestValidSubstring(self, word: str, forbidden: List[str]) -> int:
forbidden = set(forbidden)
res = 0
size = len(word)
rp = size - 1
for lp in range(size - 1, -1, -1):
for delta in range(lp, min(lp + 10, rp + 1)):
if word[lp : delta + 1] in forbidden:
rp = delta - 1
break
res = max(res, rp - lp + 1)
return res
# @lc code=end