1446. Consecutive Characters ¶ Time: $O(n)$ Space: $O(1)$ C++JavaPython 1 2 3 4 5 6 7 8 9 10 11 12 13 14class Solution { public: int maxPower(string s) { int ans = 1; int count = 1; for (int i = 1; i < s.length(); ++i) { count = s[i] == s[i - 1] ? count + 1 : 1; ans = max(ans, count); } return ans; } }; 1 2 3 4 5 6 7 8 9 10 11 12 13class Solution { public int maxPower(String s) { int ans = 1; int count = 1; for (int i = 1; i < s.length(); ++i) { count = s.charAt(i) == s.charAt(i - 1) ? count + 1 : 1; ans = Math.max(ans, count); } return ans; } } 1 2 3 4 5 6 7 8 9 10class Solution: def maxPower(self, s: str) -> int: ans = 1 count = 1 for i in range(1, len(s)): count = count + 1 if s[i] == s[i - 1] else 1 ans = max(ans, count) return ans