Alice and Bob take turns playing a game, with Alice** starting first**.
You are given a string num of even length consisting of digits and '?'
characters. On each turn, a player will do the following if there is still at least one '?' in num:
Choose an index i where num[i] == '?'.
Replace num[i] with any digit between '0' and '9'.
The game ends when there are no more '?' characters in num.
For Bob to win, the sum of the digits in the first half of num must be
equal to the sum of the digits in the second half. For Alice to win, the sums must not be equal.
For example, if the game ended with num = "243801", then Bob wins because 2+4+3 = 8+0+1. If the game ended with num = "243803", then Alice wins because 2+4+3 != 8+0+3.
Assuming Alice and Bob play optimally , return trueif Alice will win andfalseif Bob will win.
Input: num ="?3295???"Output: falseExplanation: It can be proven that Bob will always win. One possible outcome is:- Alice replaces the first '?'with'9'. num ="93295???".- Bob replaces one of the '?'in the right half with'9'. num ="932959??".- Alice replaces one of the '?'in the right half with'2'. num ="9329592?".- Bob replaces the last '?'in the right half with'7'. num ="93295927".Bob wins because 9+3+2+9=5+9+2+7.
Count the sum and number of ‘?’ in both halves. Each ‘?’ can be replaced by any digit, so the player can maximize or minimize the sum in their half. If the difference in possible maximums is not zero, Alice can always force a win.
classSolution {
publicbooleansumGame(String num) {
int n = num.length(), s1 = 0, s2 = 0, q1 = 0, q2 = 0;
for (int i = 0; i < n/2; i++) {
if (num.charAt(i) =='?') q1++;
else s1 += num.charAt(i) -'0';
}
for (int i = n/2; i < n; i++) {
if (num.charAt(i) =='?') q2++;
else s2 += num.charAt(i) -'0';
}
return 2 * (s1 - s2) != 9 * (q2 - q1);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution {
funsumGame(num: String): Boolean {
val n = num.length
var s1 = 0; var s2 = 0; var q1 = 0; var q2 = 0for (i in0 until n/2) {
if (num[i] =='?') q1++else s1 += num[i] - '0' }
for (i in n/2 until n) {
if (num[i] =='?') q2++else s2 += num[i] - '0' }
return2 * (s1 - s2) !=9 * (q2 - q1)
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution:
defsumGame(self, num: str) -> bool:
n = len(num)
s1 = s2 = q1 = q2 =0for i in range(n//2):
if num[i] =='?':
q1 +=1else:
s1 += int(num[i])
for i in range(n//2, n):
if num[i] =='?':
q2 +=1else:
s2 += int(num[i])
return2* (s1 - s2) !=9* (q2 - q1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
impl Solution {
pubfnsum_game(num: String) -> bool {
let n = num.len();
let (mut s1, mut s2, mut q1, mut q2) = (0, 0, 0, 0);
let bytes = num.as_bytes();
for i in0..n/2 {
if bytes[i] ==b'?' { q1 +=1; } else { s1 += (bytes[i] -b'0') asi32; }
}
for i in n/2..n {
if bytes[i] ==b'?' { q2 +=1; } else { s2 += (bytes[i] -b'0') asi32; }
}
2* (s1 - s2) !=9* (q2 - q1)
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
sumGame(num: string):boolean {
constn=num.length;
lets1=0, s2=0, q1=0, q2=0;
for (leti=0; i<n/2; i++) {
if (num[i] ==='?') q1++;
elses1+=+num[i];
}
for (leti=n/2; i<n; i++) {
if (num[i] ==='?') q2++;
elses2+=+num[i];
}
return2* (s1-s2) !==9* (q2-q1);
}
}