Compile error. Anyone knows why my solution doesn't pass compile check?


  • 0
    D

    Here is my code. Any improvement I could make. Thanks in advance.

    class Solution {
        public:
        bool checkValidString(string s) {
                vector<char> st;
    
             int count=0,l = s.length();
            for(int i = 0; i < l; i++)
            {
                if(s[i] != ')')
                {
                    if (s[i] == '(' || s[i] == '*'){
                        st.push_back(s[i]);
                        if (s[i] == '('){
                            count++;
                        }
                    }
                    
                }else{
                    if (st.empty()){
                        return false;
                    }
                    char c=st.back() ;
                    
                    if(c == '(')
                    {
                        st.pop_back();
                        count--;
                    }else{
                        
                        if (count == 0){
                            st.pop_back();// pop *
                        }else if (count != 0){
                            int star = 0;
                            // stack has at least 1 member
                            while(!st.empty() && st.back() == '*')
                            {
                                star++;//cout << st.back();
                                st.pop_back();
                            }
                            if(st.back() == '('){
                                st.pop_back();
                                count--;
                                // insert back star *
                                for(int k = 0; k < star; k++)
                                {
                                    st.push_back('*');
                                }
                            }
                            
                        }
                    }
                    
                }
                
            }
            int c_star =0, c_open = 0;
            while(!st.empty()){
                char v = st.back();
                st.pop_back();
                if (v == '*'){
                    c_star++;
                }else{
                    c_open++;
                }
                if (c_star< c_open){
                    return false;
                }
            }
            return true;
        }
    };
    

  • 0

    Hi @dan39, for some reason, the OJ does NOT like comments which contain certain chars such as ; or *. Your solution is compiled and accepted with the comments removed as follows:

    class Solution {
        public:
        bool checkValidString(string s) {
                vector<char> st;
    
             int count=0,l = s.length();
            for(int i = 0; i < l; i++)
            {
                if(s[i] != ')')
                {
                    if (s[i] == '(' || s[i] == '*'){
                        st.push_back(s[i]);
                        if (s[i] == '('){
                            count++;
                        }
                    }
                    
                }else{
                    if (st.empty()){
                        return false;
                    }
                    char c=st.back() ;
                    
                    if(c == '(')
                    {
                        st.pop_back();
                        count--;
                    }else{
                        
                        if (count == 0){
                            st.pop_back();
                        }else if (count != 0){
                            int star = 0;
                            // stack has at least 1 member
                            while(!st.empty() && st.back() == '*')
                            {
                                star++;
                                st.pop_back();
                            }
                            if(st.back() == '('){
                                st.pop_back();
                                count--;
                                
                                for(int k = 0; k < star; k++)
                                {
                                    st.push_back('*');
                                }
                            }
                            
                        }
                    }
                    
                }
                
            }
            int c_star =0, c_open = 0;
            while(!st.empty()){
                char v = st.back();
                st.pop_back();
                if (v == '*'){
                    c_star++;
                }else{
                    c_open++;
                }
                if (c_star< c_open){
                    return false;
                }
            }
            return true;
        }
    };
    

  • 0

    Hi @dan39, for other suggested solutions, I really like this one, it is very elegant Java code:

    https://discuss.leetcode.com/topic/103948/java-12-lines-solution-backtracking

    I also like my own solution, but I may have a biased opinion about it:

    https://discuss.leetcode.com/topic/104298/c-count-open-parens-from-left-to-right-then-right-to-left


Log in to reply
 

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