fix: p97 subtract panic.

This commit is contained in:
jackfiled 2024-08-11 11:20:41 +08:00
parent a29a200972
commit 67d1277152

View File

@ -10,28 +10,32 @@ impl Solution {
pub fn is_interleave(s1: String, s2: String, s3: String) -> bool {
let (s1, s2, s3): (Vec<char>, Vec<char>, Vec<char>) = (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 {
if i == 0 && j == 0 {
continue;
}
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]
}
}
@ -44,5 +48,6 @@ mod tests {
#[test]
fn test_97() {
assert!(Solution::is_interleave("aabcc".to_owned(), "dbbca".to_owned(), "aadbbcbcac".to_owned()));
}
}