Skip to content

1446. Consecutive Characters 👍

  • Time: $O(n)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class 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
13
class 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
10
class 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