Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1307 from zim0369/valid-parens
Browse files Browse the repository at this point in the history
Create 20-Valid-Parentheses.rs
  • Loading branch information
Ahmad-A0 authored Oct 22, 2022
2 parents 5e149e0 + b890337 commit 588d842
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 588d842

Please sign in to comment.