Skip to content

Commit 93d77ad

Browse files
authored
Merge pull request #1 from mirmujeeb9/patch-1
Valid Parentheses
2 parents a5069c9 + 535c98b commit 93d77ad

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Valid Parentheses/C++/main.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include<stack>
2+
class Solution {
3+
public:
4+
bool isValid(string s) {
5+
stack<char> stack;
6+
int a = s.length();
7+
int b = 0;
8+
map<char, char> pairs{{')', '('}, {'}', '{'}, {']', '['}};
9+
while (b < a) {
10+
if (s[b] == '(' || s[b] == '{' || s[b] == '[') {
11+
stack.push(s[b]);
12+
} else {
13+
if (stack.empty() || stack.top() != pairs[s[b]]) {
14+
return false;
15+
}
16+
stack.pop();
17+
}
18+
b++;
19+
}
20+
return stack.empty();
21+
}
22+
};

0 commit comments

Comments
 (0)