Why does this solution fail "+++++++++"?


  • 0
    S
    class Solution {
    private:
        bool canWinHelp(string&s, bool isStartingPlayer)
        {
            bool noMatch = true;
            for (int i = 1; i < s.size(); i++)
            {
                if (s[i] == '+' && s[i] == s[i-1])
                {
                    noMatch = false;
                    string t = s;
                    t[i] = t[i-1] = '-';
                    if (canWinHelp(t, !isStartingPlayer))
                    {
                        cout << "Player: " << (isStartingPlayer ? "1" : "2") << " with word: " << t << endl;
                        return true;
                    }
                }
            }
            
            if (noMatch && !isStartingPlayer)
            {
                cout << "Player 2 stuck at word " << s << endl;
                return true;
            }
                
            return false;
        }
    public:
        bool canWin(string s) {
            return canWinHelp(s, true);
        }
    };
    

    Logic: When player 2 cannot win, bubble up a true.


Log in to reply
 

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