1446. Consecutive Characters

problem

solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
int maxPower(string s) {
int count = 1, ret= 1, n =s.size();
char val = s[0];
for(int i=1;i<n;++i){
if(val == s[i]){
count++;
}
else{
ret = max(ret,count);
count =1;
val = s[i];
}
}
ret = max(ret,count);
return ret;
}
};

analysis

  • time complexity O(n)
  • space complexity O(1)