The Wording Game Problem
Description
LeetCode Problem 2868.
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:
wis lexicographically greater thanz.- If
w_1is the first letter ofwandz_1is the first letter ofz,w_1should either be equal toz_1or be the letter afterz_1in the alphabet. - For example, the word
"care"is closely greater than"book"and"car", but is not closely greater than"ant"or"cook".
A string s is lexicographically greater than a string t if in the first position where s and t differ, string s has a letter that appears later in the alphabet than the corresponding letter in t. If the first min(s.length, t.length) characters do not differ, then the longer string is the lexicographically greater one.
Example 1:
Input: a = ["avokado","dabar"], b = ["brazil"]
Output: false
Explanation: Alice must start the game by playing the word "avokado" since it's her smallest word, then Bob plays his only word, "brazil", which he can play because its first letter, 'b', is the letter after Alice's word's first letter, 'a'.
Alice can't play a word since the first letter of the only word left is not equal to 'b' or the letter after 'b', 'c'.
So, Alice loses, and the game ends.
Example 2:
Input: a = ["ananas","atlas","banana"], b = ["albatros","cikla","nogomet"]
Output: true
Explanation: Alice must start the game by playing the word "ananas".
Bob can't play a word since the only word he has that starts with the letter 'a' or 'b' is "albatros", which is smaller than Alice's word.
So Alice wins, and the game ends.
Example 3:
Input: a = ["hrvatska","zastava"], b = ["bijeli","galeb"]
Output: true
Explanation: Alice must start the game by playing the word "hrvatska".
Bob can't play a word since the first letter of both of his words are smaller than the first letter of Alice's word, 'h'.
So Alice wins, and the game ends.
Constraints:
1 <= a.length, b.length <= 10^5a[i]andb[i]consist only of lowercase English letters.aandbare lexicographically sorted.- All the words in
aandbcombined are distinct. - The sum of the lengths of all the words in
aandbcombined does not exceed10^6.
Solution
Approach: Simulation
Both lists are sorted and a player never loses anything by playing the smallest word that is closely greater than the current one, so we can simulate the game with one pointer per list.
Keep w (the last word), i and j (the next unused positions in a and b) and whose turn it is. Alice's first word a[0] is already played, so i = 1, j = 0, and Bob moves next. On a player's turn, scan forward in their list: skip words that are not closely greater than w (the first letter must equal that of w or be the next letter, and the word must be larger) and play the first one that is. If the list runs out, that player loses.
Complexity: time O(m + n), space O(1), where m and n are the lengths of a and b.
class Solution:
def canAliceWin(self, a: List[str], b: List[str]) -> bool:
i, j, k = 1, 0, 1
w = a[0]
while 1:
if k:
if j == len(b):
return True
if (b[j][0] == w[0] and b[j] > w) or ord(b[j][0]) - ord(w[0]) == 1:
w = b[j]
k ^= 1
j += 1
else:
if i == len(a):
return False
if (a[i][0] == w[0] and a[i] > w) or ord(a[i][0]) - ord(w[0]) == 1:
w = a[i]
k ^= 1
i += 1