forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSum Game.cpp
43 lines (38 loc) · 1.11 KB
/
Sum Game.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Solution {
public:
bool sumGame(string num) {
const int N = num.length();
int lDigitSum = 0;
int lQCount = 0;
int rDigitSum = 0;
int rQCount = 0;
for(int i = 0; i < N; ++i){
if(isdigit(num[i])){
if(i < N / 2){
lDigitSum += (num[i] - '0');
}else{
rDigitSum += (num[i] - '0');
}
}else{
if(i < N / 2){
++lQCount;
}else{
++rQCount;
}
}
}
// Case 0: Only digits (without '?')
if((lQCount + rQCount) == 0){
return (lDigitSum != rDigitSum);
}
// Case 1: Odd number of '?'
if((lQCount + rQCount) % 2 == 1){
return true;
}
// Case 2: Even number of '?'
int minQCount = min(lQCount, rQCount);
lQCount -= minQCount;
rQCount -= minQCount;
return (lDigitSum + 9 * lQCount / 2 != rDigitSum + 9 * rQCount / 2);
}
};