Sponsored
Sponsored
The Sliding Window approach iterates through the string keeping track of the current character streak. When the character changes, update the maximum streak length if the current streak is larger. Reset the current streak counter for the new character.
Time Complexity: O(n) since each character is processed once.
Space Complexity: O(1) as no extra space is used aside from variables.
1def maxPower(s: str) -> int:
2 max_power = 1
3 current_power = 1
4 for i in range(1, len(s)):
5 if s[i] == s[i-1]:
6 current_power += 1
7 else:
8 max_power = max(max_power, current_power)
9 current_power = 1
10 return max(max_power, current_power)
Iterates over s
to track consecutive repetitions, resetting and comparing streak counts as necessary.
The recursive approach utilizes memoization to avoid repetitive calculations as it navigates the string, exploring all possibilities for consecutive substrings.
Time Complexity: O(n)
Space Complexity: O(n) due to recursion call stack and cache storage.
1function maxPower(s) {
2 const cache =
JavaScript's recursive approach takes advantage of the Memoization strategy using a Map. This prevents redundant evaluations for the same substring, efficiently computing the power of the string.