File tree 1 file changed +39
-0
lines changed
1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ Given a string s, the power of the string is the maximum length of a
3
+ non-empty substring that contains only one unique character.
4
+
5
+ Return the power of the string.
6
+
7
+ Example:
8
+ Input: s = "leetcode"
9
+ Output: 2
10
+ Explanation: The substring "ee" is of length 2 with the character 'e' only.
11
+
12
+ Constraints:
13
+ - 1 <= s.length <= 500
14
+ - s contains only lowercase English letters.
15
+ """
16
+ #Difficulty: Easy
17
+ #333 / 333 test cases passed.
18
+ #Runtime: 36 ms
19
+ #Memory Usage: 13.7 MB
20
+
21
+ #Runtime: 36 ms, faster than 94.53% of Python3 online submissions for Consecutive Characters.
22
+ #Memory Usage: 13.7 MB, less than 88.51% of Python3 online submissions for Consecutive Characters.
23
+
24
+ class Solution :
25
+ def maxPower (self , s : str ) -> int :
26
+ result = count = 1
27
+ i = 0
28
+ j = i + 1
29
+ l = len (s ) - 1
30
+ while i < l :
31
+ if j <= l and s [j ] == s [i ]:
32
+ count += 1
33
+ j += 1
34
+ else :
35
+ result = max (result , count )
36
+ count = 1
37
+ i = j
38
+ j = i + 1
39
+ return result
You can’t perform that action at this time.
0 commit comments