Check Distances Between Same Letters - Problem

Imagine you're a quality inspector for a text formatting company! You've been given a special string where each letter appears exactly twice, and you need to verify if the spacing between identical letters meets specific requirements.

Here's what you have:

  • A string s containing only lowercase letters, where each letter appears exactly twice
  • An array distance of length 26, where distance[i] tells you how many letters should be between the two occurrences of the i-th letter of the alphabet

Your mission: Determine if the string is "well-spaced" - meaning the actual distance between each pair of identical letters matches the required distance.

Example: If s = "abaccb" and we need 1 letter between the two 'a's, we check: the 'a's are at positions 0 and 2, so there's 1 letter ('b') between them. Perfect! โœ…

Return true if the string meets all spacing requirements, false otherwise.

Input & Output

example_1.py โ€” 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: The letter 'a' appears at indices 0 and 2, with 1 letter between them (distance[0] = 1 โœ“). The letter 'b' appears at indices 1 and 5, with 3 letters between them (distance[1] = 3 โœ“). The letter 'c' appears at indices 3 and 4, with 0 letters between them (distance[2] = 0 โœ“). All conditions are satisfied.
example_2.py โ€” Invalid Case
$ 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: The letter 'a' appears at indices 0 and 1, with 0 letters between them. However, distance[0] = 1, which means we need exactly 1 letter between the two 'a's. Since 0 โ‰  1, the string is not well-spaced.
example_3.py โ€” Adjacent Letters Valid
$ Input: s = "aabb", distance = [0,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]
โ€บ Output: true
๐Ÿ’ก Note: The letter 'a' appears at indices 0 and 1 with 0 letters between (distance[0] = 0 โœ“). The letter 'b' appears at indices 2 and 3 with 0 letters between, but we need 1 letter between them (distance[1] = 1). Wait - actually 'b' at positions 2,3 has 0 letters between, but distance[1] = 1. This should be false. Let me recalculate... Actually for this to be true, we need distance = [0,0,...].

Visualization

Tap to expand
๐Ÿ…ฟ๏ธ Letter Distance Validation - Parking AnalogyASpot 0BSpot 1ASpot 2CSpot 3CSpot 4BSpot 5Required: 1 spot betweenActual: 1 spot โœ“Parking Rules Check๐Ÿ…ฐ๏ธ Car A: must have 1 empty spot between twinsโœ… Spots 0 & 2 โ†’ 1 empty spot โœ“๐Ÿ…ฑ๏ธ Car B: must have 3 empty spots between twinsโœ… Spots 1 & 5 โ†’ 3 empty spots โœ“๐Ÿ…ฒ๏ธ Car C: must have 0 empty spots between twinsโœ… Spots 3 & 4 โ†’ 0 empty spots โœ“๐Ÿ“ Process: Store first parking position in memory๐Ÿ” When twin arrives: Calculate distance instantlyโšก Immediate validation: Accept or reject the arrangement๐ŸŽฏ Result: All cars parked correctly - VALID arrangement!
Understanding the Visualization
1
First Car Arrives
When we see a car type for the first time, we record its parking spot number
2
Twin Car Arrives
When the same car type arrives again, we check if it parked at the correct distance
3
Instant Validation
We immediately know if the parking rule is followed or violated
4
Continue or Stop
If valid, continue; if invalid, reject the entire parking arrangement
Key Takeaway
๐ŸŽฏ Key Insight: Just like a parking attendant who remembers where the first car parked and checks the distance when the twin arrives, we store the first occurrence position and validate immediately on the second occurrence - achieving optimal O(n) efficiency!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the string, each character processed exactly twice (once for each occurrence)

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Hash map stores at most k unique characters where k is the number of unique letters (at most 26)

n
2n
โœ“ Linear Space

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
Asked in
Google 28 Amazon 22 Microsoft 18 Meta 15
23.2K Views
Medium Frequency
~15 min Avg. Time
847 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