原题链接在这里: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:

 class Solution {
public int minStickers(String[] stickers, String target) {
if(target == null || stickers == null){
return 0;
} int m = stickers.length;
int [][] map = new int[m][26];
for(int i = 0; i<m; i++){
for(char c : stickers[i].toCharArray()){
map[i][c - 'a']++;
}
} HashMap<String, Integer> hm = new HashMap<>();
hm.put("", 0); return dfs(map, target, hm);
} private int dfs(int [][] map, String target, Map<String, Integer> hm){
if(hm.containsKey(target)){
return hm.get(target);
} int [] tArr = new int[26];
for(char c : target.toCharArray()){
tArr[c - 'a']++;
} int res = Integer.MAX_VALUE;
for(int i = 0; i<map.length; i++){
if(map[i][target.charAt(0) - 'a'] == 0){
continue;
} StringBuilder sb = new StringBuilder();
for(int j = 0; j<26; j++){
if(tArr[j] > 0){
for(int k = 0; k<tArr[j] - map[i][j]; k++){
sb.append((char)('a' + j));
}
}
} int base = dfs(map, sb.toString(), hm);
if(base != -1){
res = Math.min(res, base + 1);
}
} res = res ==Integer.MAX_VALUE ? -1 : res;
hm.put(target, res);
return res;
}
}

最新文章

  1. Standard C 语言标准函数库介绍
  2. mybatis mapper.xml 配置文件问题(有的错误xml是不报的) 导致服务无法启动 。
  3. ubuntu下搭建lamp
  4. Git常用命令大全
  5. DDoS攻防战(一):概述
  6. VS2005中乱码问题
  7. Memo打印1
  8. Node.js可以做些什么?
  9. 用持续集成工具Travis进行构建和部署
  10. javascript中的DOM介绍(一)
  11. redis新手入门,摸不着头脑可以看看&lt;一&gt;
  12. Maven错误信息:Missing artifact jdk.tools:jdk.tools:jar:1.6
  13. !!在js中的用法
  14. docker常用命令2
  15. QSplineSeries QChartView绘制曲线
  16. callback源码分析——callbacks
  17. html5中audio的详细使用
  18. 第六种方式,python使用cached_property缓存装饰器和自定义cached_class_property装饰器,动态添加类属性(三),selnium webdriver类无限实例化控制成单浏览器。
  19. golang -- 字符串就地取反
  20. 搭建jdk环境

热门文章

  1. Zuul的使用,路由访问映射规则
  2. SWIG 3 中文手册——5. SWIG 基础知识
  3. 纯 css 打造一个小提示 tooltip
  4. Java SPI机制:ServiceLoader实现原理及应用剖析
  5. springmvc全局异常处理ControllerAdvice区分返回响应类型是页面还是JSON
  6. Linux内核调优部分参数说明
  7. Docker安装及简单使用
  8. 一段不错的代码JS的顶部轮播广告
  9. linux系统下使用nginx反向代理asp.net core,并配置免费的https证书
  10. python 库 PrettyTabble 使用与错误