Skip to content

Commit 4b09d01

Browse files
Update and rename ValidParanthesis.cpp to easy-20-valid_parentheses.cpp
1 parent b2dd749 commit 4b09d01

File tree

2 files changed

+43
-18
lines changed

2 files changed

+43
-18
lines changed

Codes/ValidParanthesis.cpp

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
2+
3+
// An input string is valid if:
4+
5+
// Open brackets must be closed by the same type of brackets.
6+
// Open brackets must be closed in the correct order.
7+
// Every close bracket has a corresponding open bracket of the same type.
8+
9+
10+
// Example 1:
11+
12+
// Input: s = "()"
13+
// Output: true
14+
15+
// Example 2:
16+
17+
// Input: s = "()[]{}"
18+
// Output: true
19+
20+
// Example 3:
21+
22+
// Input: s = "(]"
23+
// Output: false
24+
25+
26+
class Solution {
27+
public:
28+
bool isValid(string s) {
29+
stack<char> yes;
30+
yes.push(s[0]);
31+
for (int i = 1; s[i] != NULL; i++) {
32+
if(yes.empty()&&(s[i]==']'||s[i]=='}'||s[i]==')'))
33+
return false;
34+
if ((s[i] == ')' && yes.top()=='(')||(s[i] == ']' && yes.top()=='[')||(s[i] == '}' && yes.top()=='{')) {
35+
yes.pop();
36+
}
37+
else {
38+
yes.push(s[i]);
39+
}
40+
}
41+
return yes.empty();
42+
}
43+
};

0 commit comments

Comments
 (0)