classSolution{public:boolcanWin(stringcurrentState){if(memo.count(currentState))returnmemo[currentState];// If any of currentState[i:i + 2] == "++" and your friend can't win after// Changing currentState[i:i + 2] to "--" (or "-"), then you can winfor(inti=0;i+1<currentState.length();++i)if(currentState[i]=='+'&¤tState[i+1]=='+'&&!canWin(currentState.substr(0,i)+'-'+currentState.substr(i+2)))returnmemo[currentState]=true;returnmemo[currentState]=false;}private:unordered_map<string,bool>memo;};
classSolution{publicbooleancanWin(StringcurrentState){if(memo.containsKey(currentState))memo.get(currentState);// If any of currentState[i:i + 2] == "++" and your friend can't win after// Changing currentState[i:i + 2] to "--" (or "-"), then you can winfor(inti=0;i+1<currentState.length();++i)if(currentState.charAt(i)=='+'&¤tState.charAt(i+1)=='+'&&!canWin(currentState.substring(0,i)+"-"+currentState.substring(i+2))){memo.put(currentState,true);returntrue;}memo.put(currentState,false);returnfalse;}privateMap<String,Boolean>memo=newHashMap<>();}
classSolution:@functools.lru_cache(None)defcanWin(self,currentState:str)->bool:# If any of currentState[i:i + 2] == "++" and your friend can't win after# Changing currentState[i:i + 2] to "--" (or "-"), then you can winreturnany(Truefori,(a,b)inenumerate(zip(currentState,currentState[1:]))ifa=='+'andb=='+'andnotself.canWin(currentState[:i]+'-'+currentState[i+2:]))