From 90f1ea66c0cc22f72fbc7cd14eebe6b46024692a Mon Sep 17 00:00:00 2001 From: Vonfry Date: Mon, 11 Apr 2022 14:49:59 +0800 Subject: [PATCH] 10: finish --- 10/solution.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/10/solution.rs b/10/solution.rs index b23a398..3608159 100644 --- a/10/solution.rs +++ b/10/solution.rs @@ -1,10 +1,30 @@ pub struct Solution { - inputs: Vec<(str, str)> + inputs: Vec<(&'static str, &'static str)> } impl Solution { pub fn is_match(s: String, p: String) -> bool { + let s: Vec = s.chars().collect(); + let p: Vec = p.chars().collect(); + let s_len = s.len(); + let p_len = p.len(); + let match_c = |i, j| -> bool { + i != 0 && (p[j - 1] == '.' || s[i - 1] == p[j - 1]) + }; + + let mut dp = vec![vec![false; p_len + 1]; s_len + 1]; + dp[0][0] = true; + (0..=s_len).for_each(|i| { + (1..=p_len).for_each(|j| { + dp[i][j] = if p[j - 1] != '*' { + match_c(i, j) && dp[i - 1][j - 1] + } else { + match_c(i, j - 1) && dp[i - 1][j] || dp[i][j - 2] + } + }) + }); + dp[s_len][p_len] } } @@ -14,7 +34,7 @@ fn main() { }; for input in sln.inputs { - println!("input: {}", input); + println!("input: {}, {}", input.0, input.1); let output = Solution::is_match(String::from(input.0), String::from(input.1)); -- 2.45.2