2868. The Wording Game π
Description
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 thanz
.- If
w1
is the first letter ofw
andz1
is the first letter ofz
,w1
should either be equal toz1
or be the letter afterz1
in 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 <= 105
a[i]
andb[i]
consist only of lowercase English letters.a
andb
are lexicographically sorted.- All the words in
a
andb
combined are distinct. - The sum of the lengths of all the words in
a
andb
combined does not exceed106
.
Solutions
Solution 1: Simulation
We use $k$ to record whose turn it is, where $k=0$ means it is Alice's turn, and $k=1$ means it is Bob's turn. We use $i$ to record Alice's index, $j$ to record Bob's index, and $w$ to record the current word. Initially, we set $i=1$, $j=0$, and $w=a[0]$.
We perform the following steps repeatedly:
If $k=1$, we check if $j$ is equal to the length of $b$. If it is, then Alice wins and we return $true$. Otherwise, we check if the first letter of $b[j]$ is equal to the first letter of $w$. If it is, we check if $b[j]$ is greater than $w$, or if the first letter of $b[j]$ is one greater than the first letter of $w$. If either of these conditions is true, then Bob can play the $j$-th word. We set $w=b[j]$ and toggle $k$. Otherwise, Bob cannot play the $j$-th word, so we increment $j$.
If $k=0$, we check if $i$ is equal to the length of $a$. If it is, then Bob wins and we return $false$. Otherwise, we check if the first letter of $a[i]$ is equal to the first letter of $w$. If it is, we check if $a[i]$ is greater than $w$, or if the first letter of $a[i]$ is one greater than the first letter of $w$. If either of these conditions is true, then Alice can play the $i$-th word. We set $w=a[i]$ and toggle $k$. Otherwise, Alice cannot play the $i$-th word, so we increment $i$.
The time complexity is $O(m+n)$, where $m$ and $n$ are the lengths of arrays $a$ and $b$, respectively. We only need to traverse the arrays once. The space complexity is $O(1)$.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|