LeetCode 691. Stickers to Spell Word

原题链接在这里:https://leetcode.com/problems/stickers-to-spell-word/

题目:

We are given N different types of stickers. Each sticker has a lowercase English word on it.

You would like to spell out the given target string by cutting individual letters from your collection of stickers and rearranging them.

You can use each sticker more than once if you want, and you have infinite quantities of each sticker.

What is the minimum number of stickers that you need to spell out the target? If the task is impossible, return -1.

Example 1:

Input:

["with", "example", "science"], "thehat"

Output:

3

Explanation:

We can use 2 "with" stickers, and 1 "example" sticker.
After cutting and rearrange the letters of those stickers, we can form the target "thehat".
Also, this is the minimum number of stickers necessary to form the target string.

Example 2:

Input:

["notice", "possible"], "basicbasic"

Output:

-1

Explanation:

We can't form the target "basicbasic" from cutting letters from the given stickers.

Note:

  • stickers has length in the range [1, 50].
  • stickers consists of lowercase English words (without apostrophes).
  • target has length in the range [1, 15], and consists of lowercase English letters.
  • In all test cases, all words were chosen randomly from the 1000 most common US English words, and the target was chosen as a concatenation of two random words.
  • The time limit may be more challenging than usual. It is expected that a 50 sticker test case can be solved within 35ms on average.

题解:

For each sticker, count the char and corresponding multiplicity. 

Have the DFS to check if target could be composed by these chars.

DFS state needs current target, stickers mapping, and memoization HashMap. memoization records minimization count for the target string.

If memoization contains current target, return the minimum count.

Otherwise, for each sticker, if sticker contains current target first char. Then use it and minus corresponding char multiplicity.

Get the base, if base != -1, update res with base + 1.

After trying each sticker, record the res.

Note: If the sticker doesn't contain current target first char, need to continue.

Otherwise, it would keep DFS with this sticker and go to DFS infinitely.

Time Complexity: exponential.

Space: exponential. memoization could be all the combination.

AC Java: 

 1 class Solution {
 2     public int minStickers(String[] stickers, String target) {
 3         if(target == null || stickers == null){
 4             return 0;
 5         }
 6         
 7         int m = stickers.length;
 8         int [][] map = new int[m][26];
 9         for(int i = 0; i<m; i++){
10             for(char c : stickers[i].toCharArray()){
11                 map[i][c - 'a']++;
12             }
13         }
14         
15         HashMap<String, Integer> hm = new HashMap<>();
16         hm.put("", 0);
17         
18         return dfs(map, target, hm);
19     }
20     
21     private int dfs(int [][] map, String target, Map<String, Integer> hm){
22         if(hm.containsKey(target)){
23             return hm.get(target);
24         }
25         
26         int [] tArr = new int[26];
27         for(char c : target.toCharArray()){
28             tArr[c - 'a']++;
29         }
30         
31         int res = Integer.MAX_VALUE;
32         for(int i = 0; i<map.length; i++){
33             if(map[i][target.charAt(0) - 'a'] == 0){
34                 continue;
35             }
36             
37             StringBuilder sb = new StringBuilder();
38             for(int j = 0; j<26; j++){
39                 if(tArr[j] > 0){
40                     for(int k = 0; k<tArr[j] - map[i][j]; k++){
41                         sb.append((char)('a' + j));
42                     }
43                 }
44             }
45             
46             int base = dfs(map, sb.toString(), hm);
47             if(base != -1){
48                 res = Math.min(res, base + 1);
49             }
50         }
51         
52         res = res ==Integer.MAX_VALUE ? -1 : res;
53         hm.put(target, res);
54         return res;
55     }
56 }

猜你喜欢

转载自www.cnblogs.com/Dylan-Java-NYC/p/12053558.html