1564. Token Slide Duel

Two players share a strip given as the string strip; each character is either '.' (an empty cell) or 'T' (a token). On a turn, the current player picks one token and slides it to the left over one or more consecutive empty cells. Tokens cannot jump over other tokens and cannot leave the strip, so a token can never move right or land on an occupied cell.

The player who cannot make a move loses. The first player moves first and both play perfectly. Return true if the first player wins and false otherwise.

The strip can be very long, so a game search is hopeless. The intended solution notices that the gaps of empty cells in front of the tokens behave like stacked piles of a classic take-away game, and reads the winner from a single XOR in one left-to-right scan. Target O(n) time and O(n) space (or O(1) with a smarter scan).

Example 1

Input:
strip = "T"
Output:
false
Explanation:

There are no empty cells to slide into, so the first player cannot move and loses.

Example 2

Input:
strip = ".T"
Output:
true
Explanation:

The first player slides the token to the left edge and the second player is stuck.

Example 3

Input:
strip = "T.T"
Output:
true
Explanation:

The tokens have gaps 0 and 1, and the last token controls the odd step, so the first player wins.

Constraints

1 ≤ strip.length ≤ 2*105; strip[i] is '.' or 'T'

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms

Expected complexity

Time
O(n)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…