Skip to content

Commit 9044a90

Browse files
committed
Add valid_parentheses proplem
1 parent 7d02dd3 commit 9044a90

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88

99
## Problems
1010
- [Easy](./easy)
11-
- [Roman to Integer](./easy/roman_to_integer/java/src/main/java/com/anas/leetcode/easy/RomanToInteger.java), [Problem in leetcode](https://leetcode.com/problems/roman-to-integer/)
12-
- [Longest Common Prefix](./easy/longest_common_prefix/java/src/main/java/com/anas/leetcode/easy/LongestCommonPrefix.java), [Problem in leetcode](https://leetcode.com/problems/longest-common-prefix/)
11+
- [Roman to Integer](./easy/roman_to_integer/java/src/main/java/com/anas/leetcode/easy/RomanToInteger.java), [Problem in LeetCode](https://leetcode.com/problems/roman-to-integer/)
12+
- [Longest Common Prefix](./easy/longest_common_prefix/java/src/main/java/com/anas/leetcode/easy/LongestCommonPrefix.java), [Problem in LeetCode](https://leetcode.com/problems/longest-common-prefix/)
13+
- [Valid Parentheses](./easy/valid_parentheses/rust/valid_parentheses_checker/src/lib.rs), [Problem in LeetCode](https://leetcode.com/problems/valid-parentheses)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "valid_parentheses_checker"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
struct Solution {}
2+
3+
impl Solution {
4+
pub fn is_valid(s: String) -> bool {
5+
let mut open_parentheses = vec![];
6+
7+
// itrate over string
8+
for c in s.chars() {
9+
match c {
10+
'(' | '[' | '{' => open_parentheses.push(c),
11+
')' | ']' | '}' => match open_parentheses.pop() {
12+
Some(p) => {
13+
if !Self::is_close_by(p, c) {
14+
return false;
15+
}
16+
}
17+
None => return false,
18+
},
19+
_ => (),
20+
}
21+
}
22+
23+
open_parentheses.is_empty()
24+
}
25+
26+
fn is_close_by(p: char, other: char) -> bool {
27+
match p {
28+
'(' if other == ')' => true,
29+
'[' if other == ']' => true,
30+
'{' if other == '}' => true,
31+
_ => false,
32+
}
33+
}
34+
}
35+
36+
#[cfg(test)]
37+
mod tests {
38+
use super::*;
39+
40+
#[test]
41+
fn case_1() {
42+
assert_eq!(Solution::is_valid("()".to_string()), true);
43+
}
44+
45+
#[test]
46+
fn case_2() {
47+
assert_eq!(Solution::is_valid("()[]{}".to_string()), true);
48+
}
49+
50+
#[test]
51+
fn case_3() {
52+
assert_eq!(Solution::is_valid("(]".to_string()), false);
53+
}
54+
55+
#[test]
56+
fn case_4() {
57+
assert_eq!(
58+
Solution::is_valid("((([[[{{{(({({[]})}))}}}]]])))".to_string()),
59+
true
60+
);
61+
}
62+
63+
#[test]
64+
fn case_5() {
65+
assert_eq!(
66+
Solution::is_valid("((([[[{{{(({({[]})}))}}}]])))".to_string()),
67+
false
68+
);
69+
}
70+
}

0 commit comments

Comments
 (0)