1
# Runtime: 173 ms (Top 90.14%) | Memory: 14.9 MB (Top 69.01%)
2
class Solution:
3
def sumGame(self, num: str) -> bool:
4
n = len(num)
5
q_cnt_1 = s1 = 0
6
for i in range(
7
n // 2
8
): # get digit sum and question mark count for the first half of `num`
9
if num[i] == "?":
10
q_cnt_1 += 1
11
else:
12
s1 += int(num[i])
13
q_cnt_2 = s2 = 0
14
for i in range(
15
n // 2, n
16
): # get digit sum and question mark count for the second half of `num`
17
if num[i] == "?":
18
q_cnt_2 += 1
19
else:
20
s2 += int(num[i])
21
s_diff = s1 - s2 # calculate sum difference and question mark difference
22
q_diff = q_cnt_2 - q_cnt_1
23
return not (
24
q_diff % 2 == 0 and q_diff // 2 * 9 == s_diff
25
) # When Bob can't win, Alice wins

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0