forked from aswinkumarrk/data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlipGames_II.java
More file actions
29 lines (25 loc) · 735 Bytes
/
FlipGames_II.java
File metadata and controls
29 lines (25 loc) · 735 Bytes
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
package com.leetcode.problems.medium;
/**
* @author neeraj on 12/09/19
* Copyright (c) 2019, data-structures.
* All rights reserved.
*/
public class FlipGames_II {
public static void main(String[] args) {
System.out.println(canWin("++++"));
}
public static boolean canWin(String s) {
if (s == null || s.length() < 2) {
return false;
}
for (int i = 0; i < s.length() - 1; i++) {
if (s.charAt(i) == '+' && s.charAt(i + 1) == '+') {
String nextState = s.substring(0, i) + "--" + s.substring(i + 2);
if (!canWin(nextState)) {
return true;
}
}
}
return false;
}
}