diff --git a/solution/0000-0099/0010.Regular Expression Matching/README.md b/solution/0000-0099/0010.Regular Expression Matching/README.md index cc8a2face2285..a71fe63b71e1c 100644 --- a/solution/0000-0099/0010.Regular Expression Matching/README.md +++ b/solution/0000-0099/0010.Regular Expression Matching/README.md @@ -244,6 +244,49 @@ public: }; ``` +### **Rust** + +```rust +impl Solution { + #[allow(dead_code)] + pub fn is_match(s: String, p: String) -> bool { + let n = s.len(); + let m = p.len(); + let s = s.chars().collect::>(); + let p = p.chars().collect::>(); + + let mut dp = vec![vec![false; m + 1]; n + 1]; + + // Initialize the dp vector + dp[0][0] = true; + + for i in 1..=m { + if p[i - 1] == '*' { + dp[0][i] = dp[0][i - 2]; + } + } + + // Begin the actual dp process + for i in 1..=n { + for j in 1..=m { + if s[i - 1] == p[j - 1] || p[j - 1] == '.' { + dp[i][j] = dp[i - 1][j - 1]; + } + if p[j - 1] == '*' { + if j >= 2 && (s[i - 1] == p[j - 2] || p[j - 2] == '.') { + dp[i][j] = dp[i - 1][j] || dp[i][j - 2]; + } else if j >= 2 && s[i - 1] != p[j - 2] { + dp[i][j] = dp[i][j - 2]; + } + } + } + } + + dp[n][m] + } +} +``` + ### **Go** ```go diff --git a/solution/0000-0099/0010.Regular Expression Matching/README_EN.md b/solution/0000-0099/0010.Regular Expression Matching/README_EN.md index e7bdf6aae8ba4..40557c05a74b5 100644 --- a/solution/0000-0099/0010.Regular Expression Matching/README_EN.md +++ b/solution/0000-0099/0010.Regular Expression Matching/README_EN.md @@ -208,6 +208,49 @@ public: }; ``` +### **Rust** + +```rust +impl Solution { + #[allow(dead_code)] + pub fn is_match(s: String, p: String) -> bool { + let n = s.len(); + let m = p.len(); + let s = s.chars().collect::>(); + let p = p.chars().collect::>(); + + let mut dp = vec![vec![false; m + 1]; n + 1]; + + // Initialize the dp vector + dp[0][0] = true; + + for i in 1..=m { + if p[i - 1] == '*' { + dp[0][i] = dp[0][i - 2]; + } + } + + // Begin the actual dp process + for i in 1..=n { + for j in 1..=m { + if s[i - 1] == p[j - 1] || p[j - 1] == '.' { + dp[i][j] = dp[i - 1][j - 1]; + } + if p[j - 1] == '*' { + if j >= 2 && (s[i - 1] == p[j - 2] || p[j - 2] == '.') { + dp[i][j] = dp[i - 1][j] || dp[i][j - 2]; + } else if j >= 2 && s[i - 1] != p[j - 2] { + dp[i][j] = dp[i][j - 2]; + } + } + } + } + + dp[n][m] + } +} +``` + ### **Go** ```go diff --git a/solution/0000-0099/0010.Regular Expression Matching/Solution.rs b/solution/0000-0099/0010.Regular Expression Matching/Solution.rs new file mode 100644 index 0000000000000..bc77367d06801 --- /dev/null +++ b/solution/0000-0099/0010.Regular Expression Matching/Solution.rs @@ -0,0 +1,38 @@ +impl Solution { + #[allow(dead_code)] + pub fn is_match(s: String, p: String) -> bool { + let n = s.len(); + let m = p.len(); + let s = s.chars().collect::>(); + let p = p.chars().collect::>(); + + let mut dp = vec![vec![false; m + 1]; n + 1]; + + // Initialize the dp vector + dp[0][0] = true; + + for i in 1..=m { + if p[i - 1] == '*' { + dp[0][i] = dp[0][i - 2]; + } + } + + // Begin the actual dp process + for i in 1..=n { + for j in 1..=m { + if s[i - 1] == p[j - 1] || p[j - 1] == '.' { + dp[i][j] = dp[i - 1][j - 1]; + } + if p[j - 1] == '*' { + if j >= 2 && (s[i - 1] == p[j - 2] || p[j - 2] == '.') { + dp[i][j] = dp[i - 1][j] || dp[i][j - 2]; + } else if j >= 2 && s[i - 1] != p[j - 2] { + dp[i][j] = dp[i][j - 2]; + } + } + } + } + + dp[n][m] + } +} \ No newline at end of file