Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2020
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.72 KB | None | 0 0
  1. public class Solution {
  2.     /**
  3.      * @param s: the given string
  4.      * @return: if the starting player can guarantee a win
  5.      */
  6.     public boolean canWin(String s) {
  7.         return canWin(s, new HashMap<>());
  8.     }
  9.    
  10.     public boolean canWin(String s, Map<String, Boolean> memo) {
  11.         if (memo.containsKey(s))
  12.             return memo.get(s);
  13.         for (int i = 0; i < s.length() - 1; i++) {
  14.             if (s.charAt(i) == '+' && s.charAt(i + 1) == '+') {
  15.                 if (!canWin(s.substring(0, i) + "--" + s.substring(i+2))) {
  16.                     memo.put(s, true);
  17.                     return true;
  18.                 }
  19.             }
  20.         }
  21.         memo.put(s, false);
  22.         return false;
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement