-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0115_distinct_subsequences.rs
More file actions
46 lines (40 loc) · 1011 Bytes
/
s0115_distinct_subsequences.rs
File metadata and controls
46 lines (40 loc) · 1011 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#![allow(unused)]
pub struct Solution {}
impl Solution {
// O(n * m) O(n)
pub fn num_distinct(s: String, t: String) -> i32 {
let (m, n) = (s.len(), t.len());
let (mut dp, mut prev, mut s_char, mut t_char) = (
vec![0; n],
1,
s.chars().collect::<Vec<char>>(),
t.chars().collect::<Vec<char>>(),
);
for i in (0..m).rev() {
prev = 1;
for j in (0..n).rev() {
let old_dpj = dp[j];
if s_char[i] == t_char[j] {
dp[j] += prev;
}
prev = old_dpj;
}
}
dp[0]
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_115() {
assert_eq!(
Solution::num_distinct("rabbbit".to_owned(), "rabbit".to_owned()),
3
);
assert_eq!(
Solution::num_distinct("babgbag".to_owned(), "bag".to_owned()),
5
);
}
}