Skip to content

Commit b890337

Browse files
author
zim0369
committed
Create 20-Valid-Parentheses.rs
1 parent 939af48 commit b890337

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)