-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy path1446_Consecutive_Characters
More file actions
65 lines (54 loc) · 1.34 KB
/
1446_Consecutive_Characters
File metadata and controls
65 lines (54 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
Leetcode 1446: Consecutive Characters
Detailed video explanation: https://youtu.be/TlAvNEo1IIc
================================================
C++:
----
class Solution {
public:
int maxPower(string s) {
int count = 1, max_count = 1;
char prev = s[0];
for(int i = 1; i < s.length(); ++i){
if(s[i] == prev){
count++;
max_count = max(max_count, count);
} else {
count = 1;
prev = s[i];
}
}
return max_count;
}
};
Java:
-----
class Solution {
public int maxPower(String s) {
int count = 1, max_count = 1;
char prev = s.charAt(0);
for(int i = 1; i < s.length(); ++i){
if(s.charAt(i) == prev){
count++;
max_count = Math.max(max_count, count);
} else {
count = 1;
prev = s.charAt(i);
}
}
return max_count;
}
}
Python3:
--------
class Solution:
def maxPower(self, s: str) -> int:
count, max_count = 1, 1
prev = s[0]
for i in range(1, len(s)):
if s[i] == prev:
count += 1
max_count = max(max_count, count)
else:
count = 1
prev = s[i]
return max_count