forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
38 lines (38 loc) · 1.23 KB
/
Solution.java
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
class Solution {
public int minStickers(String[] stickers, String target) {
Deque<Integer> q = new ArrayDeque<>();
q.offer(0);
int ans = 0;
int n = target.length();
boolean[] vis = new boolean[1 << n];
vis[0] = true;
while (!q.isEmpty()) {
for (int t = q.size(); t > 0; --t) {
int state = q.poll();
if (state == (1 << n) - 1) {
return ans;
}
for (String s : stickers) {
int nxt = state;
int[] cnt = new int[26];
for (char c : s.toCharArray()) {
++cnt[c - 'a'];
}
for (int i = 0; i < n; ++i) {
int idx = target.charAt(i) - 'a';
if ((nxt & (1 << i)) == 0 && cnt[idx] > 0) {
nxt |= 1 << i;
--cnt[idx];
}
}
if (!vis[nxt]) {
vis[nxt] = true;
q.offer(nxt);
}
}
}
++ans;
}
return -1;
}
}