-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2272-SubstringWithLargestVariance.go
More file actions
148 lines (135 loc) · 4.87 KB
/
2272-SubstringWithLargestVariance.go
File metadata and controls
148 lines (135 loc) · 4.87 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
// 2272. Substring With Largest Variance
// The variance of a string is defined as the largest difference between the number of occurrences of any 2 characters present in the string. Note the two characters may or may not be the same.
// Given a string s consisting of lowercase English letters only, return the largest variance possible among all substrings of s.
// A substring is a contiguous sequence of characters within a string.
// Example 1:
// Input: s = "aababbb"
// Output: 3
// Explanation:
// All possible variances along with their respective substrings are listed below:
// - Variance 0 for substrings "a", "aa", "ab", "abab", "aababb", "ba", "b", "bb", and "bbb".
// - Variance 1 for substrings "aab", "aba", "abb", "aabab", "ababb", "aababbb", and "bab".
// - Variance 2 for substrings "aaba", "ababbb", "abbb", and "babb".
// - Variance 3 for substring "babbb".
// Since the largest possible variance is 3, we return it.
// Example 2:
// Input: s = "abcde"
// Output: 0
// Explanation:
// No letter occurs more than once in s, so the variance of every substring is 0.
// Constraints:
// 1 <= s.length <= 10^4
// s consists of lowercase English letters.
import "fmt"
func largestVariance(s string) int {
mp := make([]int, 26)
for _, v := range s {
mp[v - 'a'] = 1
}
mx := 0
max := func (x, y int) int { if x > y { return x; }; return y; }
search := func(a, b int) int {
res, count, valid, start := 0, 0, false, false
for _, v := range s {
val := int(v - 'a')
if val == a { count++ }
if val == b {
valid = true
if start && count >= 0 {
start = false
} else if count <= 0 {
start, count = true, -1
} else {
count--
}
}
if valid {
res = max(res, count)
}
}
return res
}
for a, _ := range mp {
for b, _ := range mp {
if a == b { continue }
mx = max(mx, search(a, b))
}
}
return mx
}
func largestVariance1(s string) int {
res, n := 0, len(s)
max := func (x, y int) int { if x > y { return x; }; return y; }
for a := 'a'; a <= 'z'; a++ {
for b := 'a'; b <= 'z'; b++ {
if a == b { continue }
f := [2]int{0, -n}
for _, c := range s {
if c == a {
f[0]++
f[1]++
} else if c == b {
f[1] = max(f[1]-1, f[0]-1)
f[0] = 0
}
res = max(res, f[1])
}
}
}
return res
}
func largestVariance2(s string) int {
var f0, f1 [26][26]int
for i := range f1 {
for j := range f1[i] {
f1[i][j] = -1 << 31
}
}
res := 0
max := func (x, y int) int { if x > y { return x; }; return y; }
for _, ch := range s {
ch -= 'a'
// 遍历到 ch 时,只需计算 a=ch 或者 b=ch 的状态,其他状态和 ch 无关,f 值不变
for i := 0; i < 26; i++ {
if i == int(ch) { continue }
// 假设出现次数最多的字母 a=ch,更新所有 b=i 的状态
f0[ch][i] = max(f0[ch][i], 0) + 1
f1[ch][i]++
// 假设出现次数最少的字母 b=ch,更新所有 a=i 的状态
f0[i][ch] = max(f0[i][ch], 0) - 1
f1[i][ch] = f0[i][ch]
res = max(res, max(f1[ch][i], f1[i][ch]))
}
}
return res
}
func main() {
// Example 1:
// Input: s = "aababbb"
// Output: 3
// Explanation:
// All possible variances along with their respective substrings are listed below:
// - Variance 0 for substrings "a", "aa", "ab", "abab", "aababb", "ba", "b", "bb", and "bbb".
// - Variance 1 for substrings "aab", "aba", "abb", "aabab", "ababb", "aababbb", and "bab".
// - Variance 2 for substrings "aaba", "ababbb", "abbb", and "babb".
// - Variance 3 for substring "babbb".
// Since the largest possible variance is 3, we return it.
fmt.Println(largestVariance("aababbb")) // 3
// Example 2:
// Input: s = "abcde"
// Output: 0
// Explanation:
// No letter occurs more than once in s, so the variance of every substring is 0.
fmt.Println(largestVariance("abcde")) // 0
fmt.Println(largestVariance("bluefrog")) // 0
fmt.Println(largestVariance("leetcode")) // 2
fmt.Println(largestVariance1("aababbb")) // 3
fmt.Println(largestVariance1("abcde")) // 0
fmt.Println(largestVariance1("bluefrog")) // 0
fmt.Println(largestVariance1("leetcode")) // 2
fmt.Println(largestVariance2("aababbb")) // 3
fmt.Println(largestVariance2("abcde")) // 0
fmt.Println(largestVariance2("bluefrog")) // 0
fmt.Println(largestVariance2("leetcode")) // 2
}