From f8f910b6063144db79f0586d9a9735c749eaa6b6 Mon Sep 17 00:00:00 2001 From: Gleb Koval Date: Mon, 6 Jun 2022 19:51:18 +0100 Subject: [PATCH] valid-parentheses --- valid-parentheses/sol.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 valid-parentheses/sol.rs diff --git a/valid-parentheses/sol.rs b/valid-parentheses/sol.rs new file mode 100644 index 0000000..09913ea --- /dev/null +++ b/valid-parentheses/sol.rs @@ -0,0 +1,21 @@ +impl Solution { + pub fn is_valid(s: String) -> bool { + let mut stack = vec![]; + s.chars() + .all(|chr| match chr { + '(' | '{' | '[' => { + stack.push(chr); + true + }, + _ => { + let opened = stack.pop() + .unwrap_or(' '); + // Hacky way to check if the brackets match. Not scalable, but works here. + if chr as u8 / 10 != opened as u8 / 10 { + return false; + } + true + } + }) && stack.len() == 0 + } +} \ No newline at end of file