classSolution { private: inlinevoidadd1times(int times[], int n){ if (n > times[2]) { times[2] = n; if (times[2] > times[1]) { swap(times[2], times[1]); if (times[1] > times[0]) { swap(times[1], times[0]); } } } }
inlineintgetTimes(int times[]){ returnmax( min(times[0], min(times[1], times[2])), max( min(times[0] - 1, times[1]), times[0] - 2 ) ); } public: intmaximumLength(string s){ int times[26][3] = {0}; int from = 0; for (int i = 1; i <= s.size(); i++) { if (i == s.size() || s[i] != s[i - 1]) { add1times(times[s[i - 1] - 'a'], i - from); from = i; } } int ans = 0; for (int i = 0; i < 26; i++) { ans = max(ans, getTimes(times[i])); } return ans ? ans : -1; } };
funcmaximumLength(s string)int { times := make([][3]int, 26) from := 0 for i := 1; i <= len(s); i++ { if i == len(s) || s[i] != s[i - 1] { add(times[s[i - 1] - 'a'][:], i - from) from = i } } ans := 0 for i := 0; i < 26; i++ { ans = max2(ans, getTimes(times[i][:])) } if ans != 0 { return ans } return-1 }
defmaximumLength(self, s: str) -> int: times = [[0, 0, 0] for _ inrange(26)] from_ = 0 for i inrange(1, len(s) + 1): if i == len(s) or s[i] != s[i - 1]: self.add(times[ord(s[i - 1]) - ord('a')], i - from_) from_ = i ans = 0 for i inrange(26): ans = max(ans, self.getTimes(times[i])) return ans if ans else -1