forked from arin2002/Hacktoberfest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode_valid_parentheses.cpp
More file actions
34 lines (33 loc) · 1015 Bytes
/
leetcode_valid_parentheses.cpp
File metadata and controls
34 lines (33 loc) · 1015 Bytes
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
class Solution {
public:
bool isValid(string str) {
stack<char> s;
for(int i=0;i<str.length();i++){
char ch = str[i];
//storing opening brackets in stack and then popping them out when similar one is found
if( (ch == '{') ||
(ch == '(') ||
(ch == '[') ){
s.push(ch);
}
else
//closing bracket ki bat ai to humne check kia cllsing and openeing
if(!s.empty()){
char top = s.top();
if( (ch == '}' && top == '{' ) ||
(ch == ')' && top == '(' ) ||
(ch == ']' && top == '[' ) ){
s.pop();
}
else
return false;
}
else //stack empty hua to vo check hi kisse krega char ko
return false;
}
if(s.empty())
return true;
else
return false;
}
};