Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions LeeCode_Problems/20.Valid_Parentheses.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

// Time Complexity: O(n);
//Space Complexity: O(n) //auxiliary space
#include<bits/stdc++.h>
using namespace std;

class Solution {
public:
bool isValid(string s) {
stack<char> stk;
int i=0;
for(int i=0;i<s.size();i++){
char ch=s[i];

if(ch=='(' || ch=='{' || ch=='['){
stk.push(ch);
}
else{
if(ch==')'){
if(stk.empty() || stk.top()!='('){
return false;
}
else{
stk.pop();
}
}
else if(ch=='}'){
if(stk.empty() || stk.top()!='{'){
return false;
}
else{
stk.pop();
}
}
else{
if(stk.empty() || stk.top()!='['){
return false;
}
else{
stk.pop();
}
}

}

}
return stk.empty();
}
};