C++ DFS solution


  • 0
    J
    class Solution {
    public:
        bool canWin(string s) {
            if (s == "") return false;
            return canMove(s);
        }    
    private:
        vector<string> generatePossibleNextMoves(string s) {
            vector<string> res;
            if(s.size()==0) return res;
            for(int i = 0;i<s.size()-1;i++)
            {
                if(s[i]=='+' && s[i+1]=='+')
                    res.push_back(s.substr(0,i)+"--"+s.substr(i+2)); 
            }
            return res;
        }
        
        bool canMove(string s)
        {
           vector<string> nextMoves = generatePossibleNextMoves(s);
           if(nextMoves.size()==0)  return false;
           for(auto nextMove:nextMoves)
           {
               if(!canMove(nextMove)) 
                   return true;
           }
           return false;    
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.