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 ...
随机推荐
- win10配置jdk1.8环境变量
1,安装好jdk之后,目录如下 2,右键计算机 - 属性 - 高级系统设置 3,环境变量 4,新增系统变量JAVA_HOME,输入内容D:\work\Program Files\Java\jdk1.8 ...
- SWIG 3 中文手册——5. SWIG 基础知识
目录 5 SWIG 基础知识 5.1 运行 SWIG 5.1.1 输入格式 5.1.2 SWIG 输出 5.1.3 注释 5.1.4 C 预处理器 5.1.5 SWIG 指令 5.1.6 解析限制 5 ...
- 【idea】【mysql】idea连接mysql
- Mysql插入text类型字段错误记录 com.mysql.jdbc.MysqlDataTruncation: Data truncation: #22001
一次插入操作报如下错误 com.mysql.jdbc.MysqlDataTruncation: Data truncation: #22001 是说字段值长度超过限制. MySQL TEXT数据类型的 ...
- Eureka和ZooKeeper的区别
首先我们先说下: RDBMS==>(MySql,Oracle,SqlServer等关系型数据库)遵循的原则是:ACID原则(A:原子性.C:一致性.I:独立性.D:持久性.). NoSql==& ...
- C++初探
//string1.cpp #include <iostream> int main() { using namespace std; ]={'d','o','g'}//这是char数组, ...
- asp.net 路由注册
webapi的路由注册 mvc的路由注册 urlRoutingModule路由
- office2016下载安装
https://jingyan.baidu.com/article/359911f5acfa4357fe030631.html
- 视频监控GB28181测试参考
说明: 1. 需要对前端摄像机和后端NVR.解码器.平台进行GB28181测试和开发的人员.本文档提供的一些测试细节,比如测试环境,SIP消息格式说明,SDP文本信息字段说明,GB28181测试注意 ...
- 架构师小跟班:教你从零开始申请和配置七牛云免费OSS对象存储(不能再详细了)
背景 之前为了练习Linux系统使用,在阿里云上低价买了一台服务器(网站首页有活动链接,传送门),心里想反正闲着也是闲着,就放了一个网站上去.现在随着数据越来越多,服务器空间越来越吃紧,我就考虑使用七 ...