forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStickers to Spell Word.java
39 lines (39 loc) · 1.54 KB
/
Stickers to Spell Word.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
39
class Solution {
HashMap<String,HashMap<Character,Integer>>map;
public int minStickers(String[] stickers, String target) {
map = new HashMap<>();
for(String sticker:stickers){
HashMap<Character,Integer> temp = new HashMap<>();
for(char ch:sticker.toCharArray())
temp.put(ch,temp.getOrDefault(ch,0)+1);
map.put(sticker,temp);
}
int count = memoization(target,new HashMap<>());
return count<1||count>=Integer.MAX_VALUE?-1:count;
}
public int memoization(String target, HashMap<String, Integer>dpmap){
if(target.length()==0)return 0;
if(dpmap.containsKey(target)) return dpmap.get(target);
int count = Integer.MAX_VALUE;
for(String str: map.keySet()){
HashMap<Character,Integer> xd = new HashMap(map.get(str));
String temp = target;
char ch = temp.charAt(0);
if(xd.containsKey(ch)){
for(int i =0;i<temp.length();i++){
ch = temp.charAt(i);
if(xd.containsKey(ch) && xd.get(ch)>0){
xd.put(ch,xd.get(ch)-1);
temp = temp.substring(0,i)+temp.substring(i+1);
i--;
}
}
if(temp.length()!=target.length()){
count = Math.min(count,1+memoization(temp,dpmap));
dpmap.put(target,count);
}
}
}
return count;
}
}