You are viewing a single comment's thread. Return to all comments →
string isBalanced(string str) { int ans=0; stack<char> s; if(str.length()%2!=0){ return "NO"; } for(auto i=0;i<str.length();i++){ char c=str[i]; if(c=='{' || c=='[' || c=='('){ s.push(c); } else if((c==']' && s.empty()) || (c=='}' && s.empty()) || (c==')' && s.empty()) ){ return "NO"; } else if((c=='}' && s.top()=='{') || (c==']' && s.top()=='[') || (c==')' && s.top()=='(')){ s.pop(); } // else{ // return "NO"; // } } if(s.empty()){ return "YES"; } else { return "NO"; } }
Seems like cookies are disabled on this browser, please enable them to open this website
Balanced Brackets
You are viewing a single comment's thread. Return to all comments →