-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFlipGame2.java
More file actions
39 lines (37 loc) · 1.1 KB
/
Copy pathFlipGame2.java
File metadata and controls
39 lines (37 loc) · 1.1 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
package leetcode;
import java.util.*;
public class FlipGame2 {
public boolean canWin(String s) {
return generatePossibleNextMoves(s,1);
}
public boolean generatePossibleNextMoves(String s,int counter) {
boolean canmove = false;
char[] array = s.toCharArray();
for(int i=0; i<array.length-1; i++){
if(array[i] == '+' && array[i+1] == '+'){
canmove = true;
flip(array,i,i+1);
String sub = new String(array);
if (generatePossibleNextMoves(sub, counter+1))
return true;
flip(array,i,i+1);
}
}
if(!canmove && counter % 2 == 0) return true;
return false;
}
private void flip(char[] array,int i, int j){
if(array[i] == '+'){
array[i] = '-';
array[j] = '-';
}
else{
array[i] = '+';
array[j] = '+';
}
}
public static void main(String[] args) {
FlipGame2 fg = new FlipGame2();
System.out.println(fg.canWin("+--++++++"));
}
}