Watch 10 video solutions for Consecutive Characters, a easy level problem involving String. This walkthrough by Knowledge Center has 13,470 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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 |