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 array fundamentals.
You are given a 0-indexed string s consisting of only lowercase English letters, where each letter in s appears exactly twice. You are also given a 0-indexed integer array distance of length 26.
Each letter in the alphabet is numbered from 0 to 25 (i.e. 'a' -> 0, 'b' -> 1, 'c' -> 2, ... , 'z' -> 25).
In a well-spaced string, the number of letters between the two occurrences of the ith letter is distance[i]. If the ith letter does not appear in s, then distance[i] can be ignored.
Return true if s is a well-spaced string, otherwise return false.
Example 1:
Input: s = "abaccb", distance = [1,3,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] Output: true Explanation: - 'a' appears at indices 0 and 2 so it satisfies distance[0] = 1. - 'b' appears at indices 1 and 5 so it satisfies distance[1] = 3. - 'c' appears at indices 3 and 4 so it satisfies distance[2] = 0. Note that distance[3] = 5, but since 'd' does not appear in s, it can be ignored. Return true because s is a well-spaced string.
Example 2:
Input: s = "aa", distance = [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] Output: false Explanation: - 'a' appears at indices 0 and 1 so there are zero letters between them. Because distance[0] = 1, s is not a well-spaced string.
Constraints:
2 <= s.length <= 52s consists only of lowercase English letters.s exactly twice.distance.length == 260 <= distance[i] <= 50Problem summary: You are given a 0-indexed string s consisting of only lowercase English letters, where each letter in s appears exactly twice. You are also given a 0-indexed integer array distance of length 26. Each letter in the alphabet is numbered from 0 to 25 (i.e. 'a' -> 0, 'b' -> 1, 'c' -> 2, ... , 'z' -> 25). In a well-spaced string, the number of letters between the two occurrences of the ith letter is distance[i]. If the ith letter does not appear in s, then distance[i] can be ignored. Return true if s is a well-spaced string, otherwise return false.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
"abaccb" [1,3,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
"aa" [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
two-sum)shortest-distance-to-a-character)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2399: Check Distances Between Same Letters
class Solution {
public boolean checkDistances(String s, int[] distance) {
int[] d = new int[26];
for (int i = 1; i <= s.length(); ++i) {
int j = s.charAt(i - 1) - 'a';
if (d[j] > 0 && i - d[j] - 1 != distance[j]) {
return false;
}
d[j] = i;
}
return true;
}
}
// Accepted solution for LeetCode #2399: Check Distances Between Same Letters
func checkDistances(s string, distance []int) bool {
d := [26]int{}
for i, c := range s {
c -= 'a'
if d[c] > 0 && i-d[c] != distance[c] {
return false
}
d[c] = i + 1
}
return true
}
# Accepted solution for LeetCode #2399: Check Distances Between Same Letters
class Solution:
def checkDistances(self, s: str, distance: List[int]) -> bool:
d = defaultdict(int)
for i, c in enumerate(map(ord, s), 1):
j = c - ord("a")
if d[j] and i - d[j] - 1 != distance[j]:
return False
d[j] = i
return True
// Accepted solution for LeetCode #2399: Check Distances Between Same Letters
impl Solution {
pub fn check_distances(s: String, distance: Vec<i32>) -> bool {
let n = s.len();
let s = s.as_bytes();
let mut d = [0; 26];
for i in 0..n {
let j = (s[i] - b'a') as usize;
let i = i as i32;
if d[j] > 0 && i - d[j] != distance[j] {
return false;
}
d[j] = i + 1;
}
true
}
}
// Accepted solution for LeetCode #2399: Check Distances Between Same Letters
function checkDistances(s: string, distance: number[]): boolean {
const d: number[] = Array(26).fill(0);
const n = s.length;
for (let i = 1; i <= n; ++i) {
const j = s.charCodeAt(i - 1) - 97;
if (d[j] && i - d[j] - 1 !== distance[j]) {
return false;
}
d[j] = i;
}
return true;
}
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.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.