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.
You are given a string s and an integer k.
Determine if there exists a substring of length exactly k in s that satisfies the following conditions:
"aaa" or "bbb").Return true if such a substring exists. Otherwise, return false.
Example 1:
Input: s = "aaabaaa", k = 3
Output: true
Explanation:
The substring s[4..6] == "aaa" satisfies the conditions.
"aaa" is 'b', which is different from 'a'."aaa".Example 2:
Input: s = "abc", k = 2
Output: false
Explanation:
There is no substring of length 2 that consists of one distinct character and satisfies the conditions.
Constraints:
1 <= k <= s.length <= 100s consists of lowercase English letters only.Problem summary: You are given a string s and an integer k. Determine if there exists a substring of length exactly k in s that satisfies the following conditions: The substring consists of only one distinct character (e.g., "aaa" or "bbb"). If there is a character immediately before the substring, it must be different from the character in the substring. If there is a character immediately after the substring, it must also be different from the character in the substring. Return true if such a substring exists. Otherwise, return false.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"aaabaaa" 3
"abc" 2
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3456: Find Special Substring of Length K
class Solution {
public boolean hasSpecialSubstring(String s, int k) {
int n = s.length();
for (int l = 0, cnt = 0; l < n;) {
int r = l + 1;
while (r < n && s.charAt(r) == s.charAt(l)) {
++r;
}
if (r - l == k) {
return true;
}
l = r;
}
return false;
}
}
// Accepted solution for LeetCode #3456: Find Special Substring of Length K
func hasSpecialSubstring(s string, k int) bool {
n := len(s)
for l := 0; l < n; {
r := l + 1
for r < n && s[r] == s[l] {
r++
}
if r-l == k {
return true
}
l = r
}
return false
}
# Accepted solution for LeetCode #3456: Find Special Substring of Length K
class Solution:
def hasSpecialSubstring(self, s: str, k: int) -> bool:
l, n = 0, len(s)
while l < n:
r = l
while r < n and s[r] == s[l]:
r += 1
if r - l == k:
return True
l = r
return False
// Accepted solution for LeetCode #3456: Find Special Substring of Length K
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3456: Find Special Substring of Length K
// class Solution {
// public boolean hasSpecialSubstring(String s, int k) {
// int n = s.length();
// for (int l = 0, cnt = 0; l < n;) {
// int r = l + 1;
// while (r < n && s.charAt(r) == s.charAt(l)) {
// ++r;
// }
// if (r - l == k) {
// return true;
// }
// l = r;
// }
// return false;
// }
// }
// Accepted solution for LeetCode #3456: Find Special Substring of Length K
function hasSpecialSubstring(s: string, k: number): boolean {
const n = s.length;
for (let l = 0; l < n; ) {
let r = l + 1;
while (r < n && s[r] === s[l]) {
r++;
}
if (r - l === k) {
return true;
}
l = r;
}
return false;
}
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.