4 lines clear C++ solution (recursion, 3ms)


  • 0
    W
        bool isValidSerialization(string s) {
            return check(s, 0) == s.size() - 1;
        }
        
        int check(const string& s, int i) {
            if (i >= s.size() || s[i] == '#') return i;
            while (s[i++] != ',' && i < s.size());
            return check(s, check(s, i) + 2);
        }
    

Log in to reply
 

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