forked from jsjtzyy/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC294_FlipGameII.java
More file actions
42 lines (38 loc) · 1.3 KB
/
LC294_FlipGameII.java
File metadata and controls
42 lines (38 loc) · 1.3 KB
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
import java.util.*;
public class LC294_FlipGameII{
public boolean canWin(String s) {
if(s == null || s.length() < 2) return false;
char[] arr = s.toCharArray();
return check(arr);
}
// Use recursion to consider all the possible cases.
// Suppose A can first move, B moves after A's move.
public boolean check( char[] arr){
boolean winFlag = true;
for(int i = 0; i < arr.length - 1; ++i){ // outer loop consider all the choices for A
if(arr[i] == arr[i + 1] && arr[i] == '+'){
arr[i] = '-';
arr[i + 1] = '-';
winFlag = true;
for(int j = 0; j < arr.length - 1; ++j){ // inner loop consider all choices for B
if(arr[j] == arr[j + 1] && arr[j] == '+'){
arr[j] = '-';
arr[j + 1] = '-';
if(!check(arr)){
winFlag = false;
}
arr[j] = '+';
arr[j + 1] = '+';
if(!winFlag) break; // if there is a case where A cannot win after B's move, then previous A's move is not acceptable.
}
}
arr[i] = '+';
arr[i + 1] = '+';
if(winFlag) { // if there is a case where B cannot win no matter what he does, then A's previous move can guarantee the win.
return true;
}
}
}
return false;
}
}