Skip to content

Commit 646040b

Browse files
committed
valid-parentheses
1 parent 2f0d644 commit 646040b

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

valid-parentheses/DaleSeo.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
impl Solution {
2+
pub fn is_valid(s: String) -> bool {
3+
let mut stack = Vec::new();
4+
for ch in s.chars() {
5+
match ch {
6+
'(' | '{' | '[' => stack.push(ch),
7+
')' => {
8+
if stack.pop() != Some('(') {
9+
return false;
10+
}
11+
}
12+
']' => {
13+
if stack.pop() != Some('[') {
14+
return false;
15+
}
16+
}
17+
'}' => {
18+
if stack.pop() != Some('{') {
19+
return false;
20+
}
21+
}
22+
_ => return false,
23+
}
24+
}
25+
stack.is_empty()
26+
}
27+
}

0 commit comments

Comments
 (0)