Skip to content

Commit

Permalink
Create 20-Valid-Parentheses.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
zim0369 committed Oct 22, 2022
1 parent 939af48 commit b890337
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions rust/20-Valid-Parentheses.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use std::collections::HashMap;

impl Solution {
pub fn is_valid(s: String) -> bool {
let mut stack: Vec<char> = Vec::new();
let opening = HashMap::from([(']', '['), (')', '('), ('}', '{')]);

for c in s.chars() {
match c {
'(' => stack.push(c),
'[' => stack.push(c),
'{' => stack.push(c),
_ => {
if stack.iter().last() == opening.get(&c) {
stack.pop();
} else {
return false;
}
}
}
}

stack.is_empty()
}
}

0 comments on commit b890337

Please sign in to comment.