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:
stickershas length in the range[1, 50].stickersconsists of lowercase English words (without apostrophes).targethas 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;
}
}
LeetCode 691. Stickers to Spell Word的更多相关文章
- 691. Stickers to Spell Word
We are given N different types of stickers. Each sticker has a lowercase English word on it. You wou ...
- [LeetCode] Stickers to Spell Word 贴片拼单词
We are given N different types of stickers. Each sticker has a lowercase English word on it. You wou ...
- [Swift]LeetCode691. 贴纸拼词 | Stickers to Spell Word
We are given N different types of stickers. Each sticker has a lowercase English word on it. You wou ...
- LeetCode691. Stickers to Spell Word
We are given N different types of stickers. Each sticker has a lowercase English word on it. You wou ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- leetcode 题解: Length of Last Word
leetcode: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', re ...
- leetcode@ [318] Maximum Product of Word Lengths (Bit Manipulations)
https://leetcode.com/problems/maximum-product-of-word-lengths/ Given a string array words, find the ...
- leetcode@ [211] Add and Search Word - Data structure design
https://leetcode.com/problems/add-and-search-word-data-structure-design/ 本题是在Trie树进行dfs+backtracking ...
- 【leetcode刷题笔记】Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
随机推荐
- google 镜像
google 镜像 http://scholar.hedasudi.com/ http://ac.scmor.com/
- express常见获取参数的方法
1.req.query 处理get请求 // GET /search?q=tobi+ferret req.query.q // => "tobi ferret" // GET ...
- 《Linux就该这么学》培训笔记_ch02_一些必须掌握的Linux命令
本文在原来作者的基础上做一些符合自己的修改.原文参考: <Linux就该这么学>培训笔记_ch02_一些必须掌握的Linux命令. 本章的内容虽然多,基本都是书本原话,但是笔记能精 ...
- pyqt助手中安装Qt帮助文档
一.个人安装环境 1.Windows7x64_SP1 2.anaconda2.5.0 + python2.7(anaconda集成,不需单独安装) 3.pyinstaller3.0 4.通过Anaco ...
- 【开源监控】Grafana介绍与安装
Grafana介绍与安装 Grafana介绍 场景:由于业务场景,有多个组织机构.需要在某个组织结构下,完成对本机构下的系统的实时监控以及可视化展示.底层已经用zabbix对监控指标做了数据的采集. ...
- sql server取日期各个组成部分的datename()函数
SQL Server中的日期类型datetime的默认格式是yyyy-mm-dd hh:mi:ss:mmm,很多时候我们可能会需要获取日期中的某个组成部分,因此SQL Server提供了一个daten ...
- react的模型:react是如何工作的?
1.jsx:语法模型,语句构建模型: 2.组件:集合模型,组件管理: 3.vdom:分层模型.渲染管理模型: 4.flux:管道模型.数据模型,状态管理模型: 整体上是一个UI系统从上到下的构建: f ...
- Linux进程启动/指令执行方式研究
1. 通过glibc api执行系统指令 0x1:system() glibc api system是linux系统提供的函数调用之一,glibc也提供了对应的封装api. system函数的原型为: ...
- 官方elasticsearch-certutiledit命令
地址:https://www.elastic.co/guide/en/elasticsearch/reference/7.5/certutil.html 语法: bin/elasticsearch-c ...
- AOP中获取自定义注解的参数值
目录 一.利用注解实现AOP的基本流程 1.1.创建一个注解,用来注解切点(pointcut) 1.2.创建一个service,使用上面定义的注解来指定切点 1.3.创建Aspect,增加业务逻辑 ...