Ctrl K

Gaps Between Letter Pairs

Easy
You get a string word of lowercase Latin letters in which every letter that appears does so exactly twice. You also get an array gaps of length 26: gaps[0] belongs to a, gaps[1] to b, and so on up to z. The string fits if, for every letter it contains, the number of letters between that letter's two occurrences equals the matching entry in gaps. Entries for letters missing from the string are ignored. Return True if the string fits, and False otherwise.
Example 1
word="kxkx", gaps=[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0] → True

There is one letter between the two ks and one between the two xs, exactly what gaps[10] and gaps[23] ask for. The other entries do not matter.

Example 2
word="bccb", gaps=[0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] → False

The two cs are side by side with 0 letters between them, but gaps[2] asks for 1.

Constraints
  • 2 ≤ len(word) ≤ 52
  • word consists of lowercase Latin letters
  • Every letter in word appears exactly twice
  • len(gaps) == 26
  • 0 ≤ gaps[i] ≤ 50
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsOne-Row Typing EasyDinner for Two EasyPhrases with Synonyms EasyWord from a License Plate EasyDistinct Morse Signals Easy
loading Python…
Input
word="kxkx", gaps=[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0]
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 30 tests on the server, hidden ones included.