add initial data

This commit is contained in:
timeshifter committed 2026-09-04 15:56:45 +02:00
1 parent 048a82b101
commit 51d935d06b
66 files changed
+1329

No files matched your search

+7
View File
@@ -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"
+6
View File
@@ -0,0 +1,6 @@
[package]
name = "valid-parentheses"
version = "0.1.0"
edition = "2024"
[dependencies]
+41
View File
@@ -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] = ['(', '[', '{'];