Flip Game II
Thoughts
Code
class Solution {
private boolean canWin(char[] str, Map<String, Boolean> cache) {
String ss = new String(str);
if (cache.containsKey(ss)) {
return cache.get(ss);
}
for (int i = 0; i < str.length - 1; i++) {
if (str[i] == '+' && str[i + 1] == '+') {
str[i] = '-';
str[i + 1] = '-';
boolean res = !canWin(str, cache);
str[i] = '+';
str[i + 1] = '+';
if (res) {
cache.put(ss, true);
return true;
}
}
}
cache.put(ss, false);
return false;
}
public boolean canWin(String s) {
return canWin(s.toCharArray(), new HashMap<>());
}
}Analysis
Last updated