Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- bool isValid(string s) {
- stack<char> st;
- int n = s.length();
- for(int i = 0; i<n; i++){
- if(s[i]=='(' || s[i]=='{' || s[i]=='['){
- st.push(s[i]);
- }
- else{
- if(st.size()!=0 &&((st.top()=='(' && s[i]==')') ||(st.top()=='{' && s[i]=='}') || (st.top()=='[' && s[i]==']'))){
- st.pop();
- }
- else{
- return false;
- }
- }
- }
- if(st.size()==0){
- return true;
- }
- return false;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement