806. The Wording Game

Alice and Bob each possess an array of unique strings—a and b, respectively—that are already sorted in lexicographical order.

They engage in a turn-based game with the following rules :

  • Starting Move : Alice begins by playing the smallest string from her array a.
  • Subsequent Moves : On each turn, the current player must select a word from their own array that is closely greater than the word most recently played. A word is considered closely greater if it is lexicographically greater than the previous word and its first letter is either the same as the first letter of that word or the immediate successor in the alphabet.
  • Losing Condition : If a player cannot choose any word that meets the above condition on their turn, that player loses the game.

Given the arrays a and b, determine whether Alice can force a win, assuming both players play optimally.

Example 1:

Input : a = ["apple", "peach"], b = ["banana", "cherry"]

Output : false

Explanation :

  • Alice starts with "apple" (her smallest word).
  • Bob must now choose a word that is closely greater than "apple". Since the first letter of "apple" is 'a', Bob’s valid choices must begin with either 'a' or 'b'. His word "banana" qualifies (it starts with 'b' and is lexicographically greater than "apple").

Example 2:

Input : a = ["beta", "gamma"], b = ["alpha", "delta"]

Output: true

Explanation :

  • Alice begins by playing "beta".
  • Bob is then required to play a word starting with 'b' (the same as "beta"'s first letter) or 'c' (the immediate successor). His words "alpha" and "delta" start with 'a' and 'd' respectively—neither of which is acceptable.
  • With no valid move available, Bob loses immediately, and Alice wins.

Now Your Turn!

Pick the correct output for the given input

Input : a = ["mango", "nectarine"], b = ["lemon", "orange"]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= a.length, b.length <= 105
  • a[i] and b[i] consist only of lowercase English letters.
  • a and b are lexicographically sorted.
  • All the words in a and b combined are distinct.
  • The sum of the lengths of all the words in a and b combined does not exceed 106.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
bool canAliceWin(vector<string>& a, vector<string>& b) {
// Your code goes here
}
};
Test Case

Input:

A
B