The Wording Game - Problem

Alice and Bob each have a lexicographically sorted array of strings named a and b respectively. They are playing a wording game with the following rules:

On each turn, the current player should play a word from their list such that the new word is closely greater than the last played word; then it's the other player's turn. If a player can't play a word on their turn, they lose.

Alice starts the game by playing her lexicographically smallest word. Given a and b, return true if Alice can win knowing that both players play their best, and false otherwise.

A word w is closely greater than a word z if the following conditions are met:

  • w is lexicographically greater than z
  • If w1 is the first letter of w and z1 is the first letter of z, w1 should either be equal to z1 or be the letter after z1 in the alphabet

For example, the word "care" is closely greater than "book" and "car", but is not closely greater than "ant" or "cook".

Input & Output

Example 1 — Alice Wins
$ Input: a = ["aa", "ab"], b = ["ba", "bb"]
Output: false
💡 Note: Alice starts with "aa". Bob can play either "ba" or "bb" (both closely greater since b = a+1). After Bob's move, Alice's remaining word "ab" cannot be played because "ab" is lexicographically smaller than both "ba" and "bb", so it's not closely greater. Alice has no valid moves and loses.
Example 2 — Bob Wins
$ Input: a = ["ca"], b = ["da", "db"]
Output: false
💡 Note: Alice starts with "ca". Bob can play "da" (closely greater: d = c+1). Alice has no more words. Bob wins.
Example 3 — Equal Start
$ Input: a = ["ba"], b = ["bb"]
Output: false
💡 Note: Alice starts with "ba". Bob can play "bb" (closely greater: same first letter 'b' and "bb" > "ba"). Alice has no moves left.

Constraints

  • 1 ≤ a.length, b.length ≤ 1000
  • 1 ≤ a[i].length, b[i].length ≤ 100
  • a and b consist of lowercase English letters only
  • All strings in a and b are distinct
  • Arrays a and b are lexicographically sorted

Visualization

Tap to expand
The Wording Game - Greedy Strategy INPUT Alice's Words (a) "aa" "ab" Bob's Words (b) "ba" "bb" A Alice B Bob Game Rules: - Word must be closely greater - Same or next letter start - Alice starts with smallest - Can't play = lose ALGORITHM STEPS 1 Alice plays "aa" Smallest word, starts 'a' 2 Bob needs 'a' or 'b' word Checks "ba", "bb" - both 'b' 3 Bob plays "ba" 'b' is after 'a' - valid! 4 Alice needs 'b' or 'c' word No 'b'/'c' words left! Game Simulation: "aa" --> "ba" --> ??? Alice has no valid move! "ab" starts with 'a' needs 'b' or 'c' start FINAL RESULT Output: true Alice WINS! Why Alice Wins: After Bob plays "ba", Alice needs a word that: - Starts with 'b' or 'c' - Is greater than "ba" Alice has no such word Key Insight: The "closely greater" constraint limits valid moves to words starting with the same letter or the next letter. Use greedy strategy: each player picks the smallest valid word to maximize options. Track letter transitions to determine if opponent can respond. Winner depends on who forces the other into an impossible state. TutorialsPoint - The Wording Game | Greedy Counting Strategy
Asked in
Google 15 Microsoft 12 Amazon 8
12.0K Views
Medium Frequency
~35 min Avg. Time
245 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