From a29a200972d082beef61bf651228783927bc2004 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sun, 11 Aug 2024 11:13:57 +0800 Subject: [PATCH] 20240811 Finished --- src/problem/mod.rs | 3 +- src/problem/p97_interleaving_string.rs | 48 ++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 src/problem/p97_interleaving_string.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 853f407..7c97390 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -200,4 +200,5 @@ mod p139_word_break; mod p300_longest_increasing_subsequence; mod p120_triangle; mod p64_minimum_path_sum; -mod p63_unique_paths_ii; \ No newline at end of file +mod p63_unique_paths_ii; +mod p97_interleaving_string; \ No newline at end of file diff --git a/src/problem/p97_interleaving_string.rs b/src/problem/p97_interleaving_string.rs new file mode 100644 index 0000000..257c06a --- /dev/null +++ b/src/problem/p97_interleaving_string.rs @@ -0,0 +1,48 @@ +/** + * [97] Interleaving String + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn is_interleave(s1: String, s2: String, s3: String) -> bool { + let (s1, s2, s3): (Vec, Vec, Vec) = (s1.chars().collect(), s2.chars().collect(), s3.chars().collect()); + let (n, m, t) = (s1.len(), s2.len(), s3.len()); + + if n + m != t { + return false; + } + + let mut dp = vec![false; m + 1]; + dp[0] = true; + + for i in 0..=n { + for j in 0..=m { + let p = i + j - 1; + + if i != 0 { + dp[j] &= s1[i - 1] == s3[p]; + } + + if j != 0 { + dp[j] |= dp[j - 1] && s2[j - 1] == s3[p]; + } + } + } + + dp[m] + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_97() { + } +}