Ctrl K

Lucky Ticket With Blanks

Medium
Alice and Bob are filling in a ticket number ticket of even length. It contains digits and ? blanks. They take turns, Alice first: on each turn the player picks any remaining ? and writes a digit from 0 to 9 in its place. The game ends when no blanks are left. If the digits of the first half of the ticket add up to the same total as the digits of the second half, the ticket is "lucky" and Bob wins. Otherwise Alice wins. If there are no blanks to begin with, the winner is decided right away. Both play perfectly. Return True if Alice wins and False if Bob wins.
Example 1
ticket="18?9" → True

There is a single blank and it goes to Alice. The left half sums to 1 + 8 = 9 and the right has 9 plus the blank, so any digit except 0 breaks the tie.

Example 2
ticket="??09" → False

Both blanks are on the left and the right sums to 0 + 9 = 9. Whatever digit d Alice writes, Bob writes 9 - d in the other blank, so the left also sums to 9.

Example 3
ticket="4422" → True
Constraints
  • 2 ≤ len(ticket) ≤ 10⁵
  • len(ticket) is even
  • ticket consists of digits and ? characters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSandwiched Chip Duel MediumWord Topping Duel HardRound Prices to a Total MediumCross Swaps of x and y MediumThe Middle Bag Medium
loading Python…
Input
ticket="18?9"
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 164 tests on the server, hidden ones included.