We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e332510 commit 65865d7Copy full SHA for 65865d7
src/easy/valid_parentheses.rs
@@ -0,0 +1,39 @@
1
+#[allow(dead_code)]
2
+pub fn is_valid(s: String) -> bool {
3
+ let mut stack = Vec::new();
4
+ for c in s.chars() {
5
+ match c {
6
+ '(' | '[' | '{' => stack.push(c),
7
+ ')' => {
8
+ if stack.pop() != Some('(') {
9
+ return false;
10
+ }
11
12
+ ']' => {
13
+ if stack.pop() != Some('[') {
14
15
16
17
+ '}' => {
18
+ if stack.pop() != Some('{') {
19
20
21
22
+ _ => unreachable!(),
23
24
25
+ stack.is_empty()
26
+}
27
+
28
+/*
29
30
+ */
31
32
+#[test]
33
+fn test_is_valid() {
34
+ assert_eq!(is_valid("()".to_string()), true);
35
+ assert_eq!(is_valid("()[]{}".to_string()), true);
36
+ assert_eq!(is_valid("(]".to_string()), false);
37
+ assert_eq!(is_valid("([)]".to_string()), false);
38
+ assert_eq!(is_valid("{[]}".to_string()), true);
39
0 commit comments