class Solution:
def maxPower(self, s: str) -> int:
powers = [None for x in range(len(s))]
powers[0] = 1
for i in range(1, len(powers)):
if s[i] == s[i - 1]:
powers[i] = powers[i - 1] + 1
else:
powers[i] = 1
return max(powers)
We generate a list of all possible powers.
The base case is:
[0] = 1, since there are no letters before.
And then for each power, it is either:
The previous power + 1 (because its a continuation of the substring + 1 char since the chars are the same)
aaab
[4, 5]
i[2] = i[2 - 1] + 1 because "a" at index 2 == "a" at index 1.
1, because it's a new substring
We then return the maximum possible value over our array.