Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"leetcode"
"abbcccddddeeeeedcba"
max-consecutive-ones)longest-continuous-increasing-subsequence)check-if-an-array-is-consecutive)count-number-of-homogenous-substrings)longest-substring-of-one-repeating-character)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1446: Consecutive Characters
class Solution {
public int maxPower(String s) {
int ans = 1, t = 1;
for (int i = 1; i < s.length(); ++i) {
if (s.charAt(i) == s.charAt(i - 1)) {
ans = Math.max(ans, ++t);
} else {
t = 1;
}
}
return ans;
}
}
// Accepted solution for LeetCode #1446: Consecutive Characters
func maxPower(s string) int {
ans, t := 1, 1
for i := 1; i < len(s); i++ {
if s[i] == s[i-1] {
t++
ans = max(ans, t)
} else {
t = 1
}
}
return ans
}
# Accepted solution for LeetCode #1446: Consecutive Characters
class Solution:
def maxPower(self, s: str) -> int:
ans = t = 1
for a, b in pairwise(s):
if a == b:
t += 1
ans = max(ans, t)
else:
t = 1
return ans
// Accepted solution for LeetCode #1446: Consecutive Characters
struct Solution;
impl Solution {
fn max_power(s: String) -> i32 {
let mut prev: (char, usize) = (' ', 0);
let mut res = 0;
for c in s.chars() {
if c == prev.0 {
prev.1 += 1;
} else {
prev = (c, 1);
}
res = res.max(prev.1);
}
res as i32
}
}
#[test]
fn test() {
let s = "leetcode".to_string();
let res = 2;
assert_eq!(Solution::max_power(s), res);
let s = "abbcccddddeeeeedcba".to_string();
let res = 5;
assert_eq!(Solution::max_power(s), res);
let s = "triplepillooooow".to_string();
let res = 5;
assert_eq!(Solution::max_power(s), res);
let s = "hooraaaaaaaaaaay".to_string();
let res = 11;
assert_eq!(Solution::max_power(s), res);
let s = "tourist".to_string();
let res = 1;
assert_eq!(Solution::max_power(s), res);
}
// Accepted solution for LeetCode #1446: Consecutive Characters
function maxPower(s: string): number {
let ans = 1;
let t = 1;
for (let i = 1; i < s.length; ++i) {
if (s[i] === s[i - 1]) {
ans = Math.max(ans, ++t);
} else {
t = 1;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.