Ctrl K

Word Topping Duel

Hard
Anna and Ben are playing a word game. Anna holds the word list first and Ben holds second; both lists are in alphabetical order, and all words across the two lists are distinct. The players take turns, each placing one word from their own list. The new word must **top** the last word played, w: - it comes after w in dictionary (lexicographic) order; - its first letter is either the same as the first letter of w or the letter right after it in the alphabet. For example, dog tops cat and dig, but it does not top apple (a and d are too far apart) or door (which comes after dog). Whoever cannot move loses. Anna goes first, and her opening move must be the alphabetically earliest word in her list. Return True if Anna wins when both play perfectly, and False otherwise.
Example 1
first=['cat', 'dove'], second=['cow'] → True

Anna opens with cat, Ben tops it with cow (same letter, later in the dictionary), Anna answers with dove (d comes right after c). Ben has nothing left to play, so Anna wins.

Example 2
first=['bell', 'cup'], second=['bird', 'dart'] → False

bell → bird → cup → dart. After dart Anna has no words left, so she loses.

Example 3
first=['moon'], second=['apple', 'kite'] → True

Both of Ben's words start with letters before m, so he cannot top moon.

Constraints
  • 1 ≤ len(first), len(second) ≤ 10⁵
  • Words are non-empty and consist of lowercase English letters
  • Each list is in alphabetical order
  • All words across both lists are distinct
  • The total length of all words is at most 10⁶
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsRise and Fall Lineup EasyRound Prices to a Total MediumThe Middle Bag MediumSame Prizes, Different Tastes MediumLucky Ticket With Blanks Medium
loading Python…
Input
first=['cat', 'dove'], second=['cow']
Expected
True

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