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 ...
随机推荐
- 解决IDEA卡顿的问题(Windows和Mac)
IDEA卡顿 最近一段时间经常会在开发的时候感觉到 IDEA 很卡,在一个类里上下滚动或者切换类文件时都能够明显的感觉到,我以为是我项目打开的太多了,毕竟内存优化已经做过了,但是今天实在是被这玩意儿卡 ...
- 高通msm8909打开debug串口
1.修改板级文件 $ cd AOSP $ vim device/qcom/msm8909/BoardConfig.mk 如下所示: 2.修改defconfig文件 $ cd kernel/arch/a ...
- jdk1.8 Stream 特性总结
不是数据结构 它没有内部存储,它只是用操作管道从 source(数据结构.数组.generator function.IO channel)抓取数据. 它也绝不修改自己所封装的底层数据结构的数据.例如 ...
- Sitecore XP 8.2 新功能
Sitecore的®体验平台™ 8.2是最全面的更新最新的一个,平衡增强现有客户,而在同一时间提供了引人注目的新功能.你可以在这里阅读新闻稿,但我想对8.2中的一些重大变化给予一些额外的关注.作为奖励 ...
- Eureka和ZooKeeper的区别
首先我们先说下: RDBMS==>(MySql,Oracle,SqlServer等关系型数据库)遵循的原则是:ACID原则(A:原子性.C:一致性.I:独立性.D:持久性.). NoSql==& ...
- NET 在一个数组中查找另一个数组所在起始位置(下标从0开始,未找到返回-1)
问题: 如果 search 在 dist 中顺序出现而不要求连续出现,那代码应该如何修改?如何计算这种匹配的可能性? 数组 search=[5,4,6],在数据 dist=[1,5,5,4,3,4,5 ...
- html页面的渲染And<script>位置的影响
周末加班敲代码的时用到了<script>标签,突然想到了一个问题:别的自测项目里面<script>我把他放在了不同位置,这里应该会对代码的执行与渲染后影响吧?于是今天专门进行了 ...
- 2 java并行基础
我们认真研究如何才能构建一个正确.健壮并且高效的并行系统. 进程与线程 进程(Process):是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础 ...
- 【开发笔记】- AbstractRoutingDataSource动态数据源切换,AOP实现动态数据源切换
AbstractRoutingDataSource动态数据源切换 上周末,室友通宵达旦的敲代码处理他的多数据源的问题,搞的非常的紧张,也和我聊了聊天,大概的了解了他的业务的需求.一般的情况下我们都是使 ...
- SQL报错注入
0x00:前言 sqli-libs第11关的报错注入,之前没有具体学习了解过,所以单独学习一下. 0x01:例子 uname=1&passwd=1' union select count(*) ...