-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path14-stickers-to-spell-word.rs
37 lines (36 loc) · 1.26 KB
/
14-stickers-to-spell-word.rs
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
// https://leetcode.cn/problems/stickers-to-spell-word/
impl Solution {
pub fn min_stickers(stickers: Vec<String>, target: String) -> i32 {
let m = target.len();
let mut f = vec![-1; 1 << m];
f[0] = 0;
fn dp(mask: usize, f: &mut Vec<i32>, stickers: &Vec<String>, target: &String) -> i32 {
if f[mask] != -1 {
return f[mask];
}
f[mask] = target.len() as i32 + 1;
for x in stickers {
let mut left = mask;
let mut cnt = [0; 26];
for c in x.bytes() {
cnt[(c - b'a') as usize] += 1;
}
for (i, c) in target.bytes().enumerate() {
if mask >> i & 1 == 1 && cnt[(c - b'a') as usize] > 0 {
cnt[(c - b'a') as usize] -= 1;
left ^= 1 << i;
}
}
if left < mask {
f[mask] = f[mask].min(dp(left, f, stickers, target) + 1);
}
}
f[mask]
}
let ans = dp((1 << m) - 1, &mut f, &stickers, &target);
if ans < m as i32 {
return ans;
}
-1
}
}