-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2781-LengthOfTheLongestValidSubstring.go
More file actions
90 lines (77 loc) · 3.49 KB
/
2781-LengthOfTheLongestValidSubstring.go
File metadata and controls
90 lines (77 loc) · 3.49 KB
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
82
83
84
85
86
87
88
89
90
package main
// 2781. 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:
// 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 <= 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.
import "fmt"
func longestValidSubstring(word string, forbidden []string) int {
mp := make(map[string]bool)
for _, v := range forbidden {
mp[v] = true
}
max := func (x, y int) int { if x > y { return x; }; return y; }
res, n := 0, len(word)
for i, j := 0, 0; j < n; j++ {
for k := j; k > max(j - 10, i - 1); k-- {
if mp[word[k:j + 1]] {
i = k + 1
break
}
}
res = max(res, j - i + 1)
}
return res
}
func longestValidSubstring1(word string, forbidden []string) int {
mp := make(map[string]bool, len(forbidden))
for _, v := range forbidden {
mp[v] = true
}
max := func (x, y int) int { if x > y { return x; }; return y; }
res, l := 0, 0
for r := range word {
for i := r; i >= l && i > r - 10; i-- {
if mp[word[i:r + 1]] {
l = i + 1
break
}
}
res = max(res, r - l + 1)
}
return res
}
func main() {
// 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.
fmt.Println(longestValidSubstring("cbaaaabc", []string{"aaa","cb"})) // 4
// 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.
fmt.Println(longestValidSubstring("leetcode", []string{"de","le","e"})) // 4
fmt.Println(longestValidSubstring("bluefrog", []string{"de","le","e"})) // 4
fmt.Println(longestValidSubstring1("cbaaaabc", []string{"aaa","cb"})) // 4
fmt.Println(longestValidSubstring1("leetcode", []string{"de","le","e"})) // 4
fmt.Println(longestValidSubstring1("bluefrog", []string{"de","le","e"})) // 4
}