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
scontaining only lowercase letters, where each letter appears exactly twice - An array
distanceof length 26, wheredistance[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
Visualization
Time & Space Complexity
Single pass through the string, each character processed exactly twice (once for each occurrence)
Hash map stores at most k unique characters where k is the number of unique letters (at most 26)
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