Check Distances Between Same Letters - Problem

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.

Input & Output

Example 1 — Basic Valid Case
$ 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
💡 Note: Character 'a' appears at positions 0,2 with 1 character between (distance[0]=1 ✓). Character 'b' appears at positions 1,5 with 3 characters between (distance[1]=3 ✓). Character 'c' appears at positions 3,4 with 0 characters between (distance[2]=0 ✓).
Example 2 — Invalid Distance
$ 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
💡 Note: Character 'a' appears at positions 0,1 with 0 characters between, but distance[0]=1 requires 1 character between them.
Example 3 — Perfect Adjacent Match
$ Input: s = "aa", distance = [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,0]
Output: true
💡 Note: Character 'a' appears at positions 0,1 with 0 characters between, matching distance[0]=0 exactly.

Constraints

  • 2 ≤ s.length ≤ 52
  • s consists only of lowercase English letters
  • Each letter appears in s exactly twice
  • distance.length == 26
  • 0 ≤ distance[i] ≤ 50

Visualization

Tap to expand
Check Distances Between Same Letters INPUT String s = "abaccb" a 0 b 1 a 2 c 3 c 4 b 5 distance array: 1 a 3 b 0 c 5 d ... e-z Expected distances: 'a': 1 letter between 'b': 3 letters between 'c': 0 letters between ALGORITHM STEPS 1 Create Hash Map Store first index of each char 2 Iterate String For each character at index i 3 Check or Store If seen: verify distance Else: store index 4 Validate Distance i - first - 1 == distance[c] Hash Map State 'a' : 0 (idx 2-0-1=1) 'b' : 1 (idx 5-1-1=3) 'c' : 3 (idx 4-3-1=0) All distances match! FINAL RESULT Distance Verification: 'a': pos 0,2 2-0-1 = 1 == distance[0] OK 'b': pos 1,5 5-1-1 = 3 == distance[1] OK 'c': pos 3,4 4-3-1 = 0 == distance[2] OK Output: true String is well-spaced! All letters match expected distance values Key Insight: Use a hash map to store the first occurrence index of each character. When encountering the same character again, calculate the actual distance as (current_index - first_index - 1) and compare with the expected distance. This achieves O(n) time complexity with a single pass through the string. TutorialsPoint - Check Distances Between Same Letters | Hash Map - Single Pass Approach
Asked in
Google 12 Meta 8 Microsoft 6
12.5K Views
Medium Frequency
~15 min Avg. Time
445 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen