diff --git a/valid-parentheses/DaleSeo.rs b/valid-parentheses/DaleSeo.rs new file mode 100644 index 000000000..0ea456041 --- /dev/null +++ b/valid-parentheses/DaleSeo.rs @@ -0,0 +1,27 @@ +impl Solution { + pub fn is_valid(s: String) -> bool { + let mut stack = Vec::new(); + for ch in s.chars() { + match ch { + '(' | '{' | '[' => stack.push(ch), + ')' => { + if stack.pop() != Some('(') { + return false; + } + } + ']' => { + if stack.pop() != Some('[') { + return false; + } + } + '}' => { + if stack.pop() != Some('{') { + return false; + } + } + _ => return false, + } + } + stack.is_empty() + } +}