LC 20. Valid Parentheses

Nilanjan Deb · April 5, 2020

This is my CPP solution.

class Solution {
public:
    bool isValid(string s) {
        int n = s.size();
        if(n==0){
            return true;
        }
        if(n==1){
            return false;
        }
        char store[n+1] ;
        int store_count = 0;
        for(int i=0;i<n;i++){
            if(s[i]=='(' || s[i] == '{' || s[i] == '['){
                store[store_count++] = s[i];
            }
            else{
                if(store_count>0 && ((s[i] == ')' && store[store_count-1] == '(') 
                || (s[i] == '}' && store[store_count-1] == '{') || (s[i] == ']' && store[store_count-1] == '['))){
                    store_count--;
                    continue;
                }
                else{
                    return false;
                }
            }
        }
        if(store_count){
            return false;
        }
        return true;
    }
};


Dicussion Forum