-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathParenthesis Checker.cpp
38 lines (37 loc) · 958 Bytes
/
Parenthesis Checker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Solution
{
public:
//Function to check if brackets are balanced or not.
bool ispar(string x)
{
// Your code here
stack <char> st;
char temp;
map<char,char> mp;
mp['}']='{';
mp[')']='(';
mp[']']='[';
map<char,char>::iterator itr;
for(int i=0;i<x.size();i++){
if(x[i]=='{' || x[i]=='(' || x[i]=='['){
st.push(x[i]);
}
if(x[i]=='}' || x[i]==')' || x[i]==']'){
if(st.empty()){
return false;
}
else{
temp=st.top();
itr=mp.find(x[i]);
if(temp==itr->second){
st.pop();
}
else{
return false;
}
}
}
}
return st.empty()?true:false;
}
};