Accepted simple C++ solution without using stack!


  • 0
    L
    class Solution {
    public:
        bool isValid(string s) {
            if (s.size() % 2 == 1 ) return false;
            int i, eraseFlag, sSize;
            while (1) {
                eraseFlag = 0;
                sSize=s.size();
                for (i=0; i< (sSize-1) ; ++i) {
                    if (s[i+1]-s[i] == 1 || s[i+1]-s[i] == 2) {
                        s.erase(i,2);
                        eraseFlag =1;
                        break;
                    }
                }
                if (eraseFlag == 0) {
                    break;
                }
            }
    
            if (s.size() == 0) return true;
            else return false;
        }
    };

  • 0
    C

    I am a little bit worrying the performance of string::erase.


Log in to reply
 

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