Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Solution {
- /**
- * @param s: the given string
- * @return: if the starting player can guarantee a win
- */
- public boolean canWin(String s) {
- return canWin(s, new HashMap<>());
- }
- public boolean canWin(String s, Map<String, Boolean> memo) {
- if (memo.containsKey(s))
- return memo.get(s);
- for (int i = 0; i < s.length() - 1; i++) {
- if (s.charAt(i) == '+' && s.charAt(i + 1) == '+') {
- if (!canWin(s.substring(0, i) + "--" + s.substring(i+2))) {
- memo.put(s, true);
- return true;
- }
- }
- }
- memo.put(s, false);
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement