Skip to content

Commit 588d842

Browse files
authored
Merge pull request #1307 from zim0369/valid-parens
Create 20-Valid-Parentheses.rs
2 parents 5e149e0 + b890337 commit 588d842

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

rust/20-Valid-Parentheses.rs

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

0 commit comments

Comments
 (0)