add new solutions
This commit is contained in:
parent
489fa73880
commit
0f5f9e331c
11 changed files with 539 additions and 0 deletions
35
solutions/37/q3713/solution.go
Normal file
35
solutions/37/q3713/solution.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package q3713
|
||||
|
||||
func longestBalanced(s string) int {
|
||||
longest := 0
|
||||
cnt, tmp := [26]int{}, [26]int{}
|
||||
|
||||
for i := range len(s) {
|
||||
copy(cnt[:], tmp[:])
|
||||
distinct, imbal, maxCnt := 0, 0, 0
|
||||
|
||||
for j := i; j >= 0; j-- {
|
||||
c := s[j] - 'a'
|
||||
cnt[c]++
|
||||
if cnt[c] == 1 {
|
||||
distinct++
|
||||
imbal++
|
||||
}
|
||||
|
||||
if cnt[c] > maxCnt {
|
||||
maxCnt = cnt[c]
|
||||
imbal = distinct - 1
|
||||
} else if cnt[c] == maxCnt {
|
||||
imbal--
|
||||
}
|
||||
|
||||
if imbal == 0 {
|
||||
longest = max(longest, i-j+1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return longest
|
||||
}
|
||||
|
||||
var _ = longestBalanced
|
||||
Loading…
Add table
Add a link
Reference in a new issue