My C++ accepted solution. Not the cleanest one though!


  • 0
    D
    class Solution {
    public:
        bool isValid(string code) {
            stack<string> tagc;
            string tagname = "";
            bool first = true;
            for (int i=0; i<code.length(); ++i)
            {
                if (code[i] == '<' && (i+3 >code.length()))
                    return false;
                    
                if (code[i] == '<' && (i != code.length()-1) && (code[i+1] != '/') && (code[i+1] != '!'))
                {
                    
                    if (!first && tagc.empty())
                        return false;
                    first = false;
                    ++i;
                    tagname = "";
                    while (i<code.length() && code[i] != '>' )
                    {
                        
                        if (!isupper(code[i]))
                            return false;
                            
                        tagname.push_back(code[i]);
                        ++i;
                    }
                    
                    if (i == code.length() || (tagname.length() <1) || (tagname.length() >9))
                        return false;
    
                    tagc.push(tagname);
                    
                }
                else if ((code[i] == '<') && (i != (code.length()-1)) && code[i+1] == '/')
                {
                    i += 2;
                    tagname = "";
                    while ((i<code.length()) && (code[i] != '>') )
                    {
                        if (!isupper(code[i]))
                            return false;
                            
                        tagname.push_back(code[i]);
                        ++i;
                        
                    }
                    
                    if (i == code.length())
                        return false;
                        
                    if (tagc.empty() || tagname != tagc.top())
                        return false;
                        
                    tagc.pop();
                    
                }
                else if (code[i] == '<' && (i != code.length()-1) && code[i+1] == '!')
                {
                    i += 2;
                    
                    if (code.substr(i,7) != "[CDATA[")
                        return false;
                        
                    i += 7;
                    
                    tagname = "";
                    while ((i+2)<code.length() && (code.substr(i,3) != "]]>") )
                    {
                        tagname.push_back(code[i]);
                        ++i;
                    }
                    
                    if (code.substr(i,3) != "]]>" || tagc.empty())
                        return false;
                    
                    i += 2;     
                    
                    
                }
                else if (tagc.empty())
                    return false;
            }
            
            if (!tagc.empty())
                return false;
            
            return true;
        }
    };
    
    

Log in to reply
 

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