class Solution:
def sumGame(self, num: str) -> bool:
n = len(num)
sum1 = sum2 = cnt1 = cnt2 = 0
for i in range(n):
if i & 1 == 0:
if num[i] == '?':
cnt1 += 1
else:
sum1 += int(num[i])
else:
if num[i] == '?':
cnt2 += 1
else:
sum2 += int(num[i])
# If number of question marks is odd, Alice can always win
if (cnt1 + cnt2) % 2 == 1:
return True
# Otherwise, check if Alice can force a win
diff = sum1 - sum2
# Each ? in left/right can be replaced by 0~9, but their sum difference matters
# Alice moves first, so she can always maximize the difference
# If (cnt1 - cnt2) * 9 // 2 != diff, then Alice can win
return diff != (cnt2 - cnt1) * 9 // 2
class Solution {
public:
bool sumGame(string num) {
int n = num.size();
int sum1 = 0, sum2 = 0, cnt1 = 0, cnt2 = 0;
for (int i = 0; i < n; ++i) {
if (i % 2 == 0) {
if (num[i] == '?') cnt1++;
else sum1 += num[i] - '0';
} else {
if (num[i] == '?') cnt2++;
else sum2 += num[i] - '0';
}
}
if ((cnt1 + cnt2) % 2 == 1) return true;
int diff = sum1 - sum2;
return diff != (cnt2 - cnt1) * 9 / 2;
}
};
class Solution {
public boolean sumGame(String num) {
int n = num.length();
int sum1 = 0, sum2 = 0, cnt1 = 0, cnt2 = 0;
for (int i = 0; i < n; ++i) {
if (i % 2 == 0) {
if (num.charAt(i) == '?') cnt1++;
else sum1 += num.charAt(i) - '0';
} else {
if (num.charAt(i) == '?') cnt2++;
else sum2 += num.charAt(i) - '0';
}
}
if ((cnt1 + cnt2) % 2 == 1) return true;
int diff = sum1 - sum2;
return diff != (cnt2 - cnt1) * 9 / 2;
}
}
var sumGame = function(num) {
let n = num.length;
let sum1 = 0, sum2 = 0, cnt1 = 0, cnt2 = 0;
for (let i = 0; i < n; ++i) {
if (i % 2 === 0) {
if (num[i] === '?') cnt1++;
else sum1 += parseInt(num[i]);
} else {
if (num[i] === '?') cnt2++;
else sum2 += parseInt(num[i]);
}
}
if ((cnt1 + cnt2) % 2 === 1) return true;
let diff = sum1 - sum2;
return diff !== (cnt2 - cnt1) * 9 / 2;
};
You are given a string num
consisting of an even number of digits and question marks ('?'). The string represents a number split into two equal halves. Alice and Bob take turns replacing a question mark with a digit from 0 to 9, with Alice going first. Once all question marks are replaced, if the sum of the digits in the first half equals the sum in the second half, Bob wins. Otherwise, Alice wins. Both players play optimally.
Key constraints:
num
is always of even length.true
if Alice wins, false
if Bob wins.The first step is to recognize that this is a two-player game with optimal play. At first glance, it might seem necessary to simulate every possible move, but this quickly becomes infeasible due to the exponential number of choices for each question mark.
Instead, we can focus on the sums of each half and the number of question marks in each half. Since each player picks digits optimally, Alice will maximize the difference between the two halves, while Bob will try to minimize it. The key is to determine whether Alice can always force the sums to be unequal, regardless of Bob's actions.
By observing the structure, we see that the two halves can be analyzed separately: count the sum of known digits and the number of question marks in each half. The outcome depends on whether the difference in possible sums can be balanced, given the number of question marks and the order of play.
num
and for each half, count:
(cnt2 - cnt1) * 9 / 2
, where cnt1
and cnt2
are the number of question marks in each half.True
if Alice can win, otherwise False
.
This approach avoids brute-force simulation and instead leverages mathematical reasoning about the sums and question mark distribution.
Example Input: num = "2?9??0"
Another Example: num = "?329?6"
num
.
The Sum Game problem can be solved efficiently by analyzing the distribution of question marks and the sums of known digits in each half. By recognizing that the game reduces to a matter of maximizing or minimizing the sum difference with optimal play, we avoid brute-force simulation and achieve a linear time solution. The key insight is that Alice can always win if the total number of question marks is odd, or if the difference in known digit sums cannot be balanced by the available question marks. This makes the solution both elegant and efficient.