The power of the string is the maximum length of a non-empty substring that contains only one unique character.
Given a string s, return the power of s.
Example 1:
Input: s = "leetcode" Output: 2 Explanation: The substring "ee" is of length 2 with the character 'e' only.
Example 2:
Input: s = "abbcccddddeeeeedcba" Output: 5 Explanation: The substring "eeeee" is of length 5 with the character 'e' only.
Constraints:
1 <= s.length <= 500s consists of only lowercase English letters.Problem Overview: Given a string s, return the length of the longest substring that contains only one repeating character. The task is essentially to compute the maximum run length of identical characters while scanning the string.
This problem belongs to the string processing category and is often used to test whether you can efficiently track patterns while iterating through characters.
Approach 1: Sliding Window / Linear Scan (O(n) time, O(1) space)
The optimal approach scans the string once while tracking the current streak of identical characters. Start with two variables: currentCount for the length of the current run and maxCount for the best result seen so far. Iterate through the string from index 1. If the current character matches the previous character, increment currentCount. Otherwise reset currentCount to 1 because a new character sequence begins. After each step, update maxCount = max(maxCount, currentCount).
This method behaves like a simplified sliding window where the window expands while characters match and resets when they change. Since each character is processed exactly once, the algorithm runs in O(n) time with O(1) extra space. This is the most common and practical solution for the problem.
Approach 2: Recursive Approach with Memoization (O(n) time, O(n) space)
You can also solve the problem using recursion by defining a function that computes the longest run ending at a given index. If s[i] == s[i-1], the run length becomes 1 + f(i-1); otherwise it resets to 1. Memoization stores results for each index so repeated computations are avoided.
While this version still processes each index once, recursion introduces stack overhead and requires an auxiliary memo array or dictionary, resulting in O(n) space usage. The logic mirrors the iterative scan but expresses it in a top‑down dynamic style. This approach can be useful when practicing recursion patterns or when building intuition about state transitions across indices.
Recommended for interviews: The sliding window / linear scan approach is what interviewers expect. It demonstrates that you can detect contiguous patterns while iterating through a string and maintain constant space. The recursive version shows conceptual understanding, but the iterative O(n) scan is cleaner, faster, and typically preferred in production code.
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.
The code initializes two counters: max_power and current_power. As it iterates through the string, if consecutive characters are identical, it increments current_power. If not, it updates max_power if current_power exceeds it, and resets current_power.
Time Complexity: O(n) since each character is processed once.
Space Complexity: O(1) as no extra space is used aside from variables.
The recursive approach utilizes memoization to avoid repetitive calculations as it navigates the string, exploring all possibilities for consecutive substrings.
Python's recursive solution uses a cache to store already computed results, progressively calculating the power from the leftmost to the rightmost position of the string. It returns the maximum possible achieved by counting same consecutive characters.
Python
JavaScript
Time Complexity: O(n)
Space Complexity: O(n) due to recursion call stack and cache storage.
We define a variable t to represent the length of the current consecutive characters, initially t=1.
Next, we traverse the string s starting from the second character. If the current character is the same as the previous character, then t = t + 1, and update the answer ans = max(ans, t); otherwise, set t = 1.
Finally, return the answer ans.
The time complexity is O(n), where n is the length of the string s. The space complexity is O(1).
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Sliding Window Approach | Time Complexity: O(n) since each character is processed once. |
| Recursive Approach with Memoization | Time Complexity: O(n) |
| Traversal and Counting | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Sliding Window / Linear Scan | O(n) | O(1) | Best general solution when scanning strings for consecutive patterns |
| Recursive with Memoization | O(n) | O(n) | Useful for practicing recursion or dynamic state transitions across indices |
Consecutive Characters | LeetCode 1446 | C++, Java, Python • Knowledge Center • 13,470 views views
Watch 9 more video solutions →Practice Consecutive Characters with our built-in code editor and test cases.
Practice on FleetCode