-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlargest_variance.go
51 lines (47 loc) · 994 Bytes
/
largest_variance.go
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
package problem2272
/*
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.
*/
func largestVariance(s string) int {
var res int
var freq [26]int
for i := range s {
freq[s[i]-'a']++
}
for a := range freq {
for b := range freq {
var cA, cB int
var rA, rB = freq[a], freq[b]
if a == b || rA == 0 || rB == 0 {
continue
}
for i := range s {
ch := int(s[i] - 'a')
if ch == b {
cB++
}
if ch == a {
cA++
rA--
}
if cA > 0 {
res = max(res, cB-cA)
}
if cB < cA && rA >= 1 {
cA, cB = 0, 0
}
}
}
}
return res
}
func max(a, b int) int {
if a > b {
return a
}
return b
}