12345678910111213141516171819202122232425262728293031323334353637383940 |
- package main
- func main() {
- println(getAns("ABAAABBBA"))
- println(getAns("ABAAAABBBA"))
- println(getAns("ABAAAAABBBA"))
- println(getAns("AAAAAA"))
- println(getAns("BABAABBABABABAAABAABABABBBAAABBAAABAABBBABBAAABABBBBBBBBBBBBAABBBBAAABAAAAABBBBBABBABBAABABAAABBABAABBAAAABBAABBBABABABABABBBAAABBBABAABAAAABBBBBAABBBABBBBAAABBABBAAABBABBABBBBBBBBBBBBBAABBAAAABAABABAABBBABBAAAAAABAABAABABBABAABBAAABBBBAABABBBABABAABAAABABBBBAABBAAABBBBBBABBAABAABABBBBBBBABBAAAABBABABBBBBBABABAABBABAABABBBBABBABBAABBAABBABABABBBAABBAABAABAABBBBBBBBBABAAABBAAAABBBAABABABBABAAAABABBABBAABAAABBBBBBAABABAAABAAABBABAABAABAAAABABAABABABAABABAAABBBBBAAAAAABAABBBABBBABBBBBBAAABABAAAABABAAAAAABABAABABBBABAAAAABAABBBAABBBAAAABAAAAABBBABABABBBAABAAAAAABABBBABABBBABBABABBBBABABBABABBBABABBAAAABAABBBAABABABABABAAABAABBAAAABAAAAABAABBBBABABBBBAABAABBBBABBAAAAAAAAAABBBAAABAAABBAAAAAAABBAAAAAABAAABBAABABABABBAAAAAABABAAABABABBBABAABBBABAABBAAAABBBBAAAABBAAAAABABBBABBAAABABABBAABABAABBBBAABAAABAABBAAAAAABAAABBBAAAABBABABAABABAAABBAABBABABBBAABBABBAABABABAAABABBAABBABABABBABBBABAAAAAABBAABBBAAABAAABAAABBBBBABBABBBABBBAABBABAAAAABBABABBABAAAAAABBBABABBABAABABAABBAAAAABABBAAABABABABBBAABB"))
- }
- func getAns(S string) int {
- length := len(S)
- sum := 0
- sumArr := make([]int, length)
- posMap := make(map[int]int)
- for i := 0; i < length; i++ {
- if S[i] == 'A' {
- sum++
- } else {
- sum--
- }
- sumArr[i] = sum
- posMap[sum] = i
- }
- if sum == 0 {
- return length
- } else if sum == length || sum == -length {
- return 0
- }
- maxLen := 0
- for i := 0; i < length; i++ {
- if sumArr[i] != 0 && maxLen < posMap[sumArr[i]]-i {
- maxLen = posMap[sumArr[i]] - i
- }
- }
- if posMap[0] != posMap[len(posMap)-1] && posMap[0]+1 > maxLen {
- maxLen = posMap[0] + 1
- }
- return maxLen
- }
|