add initial data
This commit is contained in:
1 parent
048a82b101
commit
51d935d06b
66 files changed
+1329
No files matched your search
Generated
+7
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "valid-parentheses"
|
||||
version = "0.1.0"
|
||||
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "valid-parentheses"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
@@ -0,0 +1,41 @@
|
||||
fn main() {
|
||||
dbg!(is_valid("()".to_string()));
|
||||
dbg!(is_valid("()[]{}".to_string()));
|
||||
dbg!(is_valid("(]".to_string()));
|
||||
dbg!(is_valid("([])".to_string()));
|
||||
dbg!(is_valid("([)])".to_string()));
|
||||
}
|
||||
|
||||
fn is_valid(s: String) -> bool {
|
||||
let mut stack = vec![];
|
||||
for c in s.chars() {
|
||||
if OPEN.contains(&c) {
|
||||
stack.push(c);
|
||||
} else {
|
||||
let last = stack.last();
|
||||
if last.is_none() {
|
||||
return false;
|
||||
}
|
||||
if matches(*last.unwrap(), c) {
|
||||
stack.pop();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if !stack.is_empty() {
|
||||
return false;
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
fn matches(c1: char, c2: char) -> bool {
|
||||
(c1 == '(' && c2 == ')')
|
||||
|| (c1 == ')' && c2 == '(')
|
||||
|| (c1 == '[' && c2 == ']')
|
||||
|| (c1 == ']' && c2 == '(')
|
||||
|| (c1 == '{' && c2 == '}')
|
||||
|| (c1 == '}' && c2 == '{')
|
||||
}
|
||||
|
||||
const OPEN: [char; 3] = ['(', '[', '{'];
|
||||
Reference in new issue
Block a user