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: true
💡 Note: Alice starts with "aa". Bob can play "ba" (closely greater). Alice can then play "ab" (closely greater than "ba"). Bob has no valid moves since "bb" is not closely greater than "ab" (b+1=c, but "bb" < "cb").
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: Turn-Based StrategyAlice["aa","ab"]Starts: "aa"Bob["ba","bb"]RespondsClosely Greater Rulew > z AND (w[0] == z[0] OR w[0] == z[0]+1)Example: "ba" > "aa" and b = a+1 ✓Turn 1Alice: "aa"Turn 2Bob: "ba"Turn 3Alice: "ab"Turn 4Bob: No moves!Result: Alice Wins! 🎉Bob cannot play "bb" because it\'s not closely greater than "ab"
Understanding the Visualization
1
Setup
Alice has ["aa","ab"], Bob has ["ba","bb"]
2
Game Play
Alice starts "aa" → Bob plays "ba" → Alice plays "ab"
3
Victory
Bob has no valid moves, Alice wins
Key Takeaway
🎯 Key Insight: Success depends on forcing the opponent into positions with no valid closely greater words
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