691. 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.
Approach #1: DP. [C++]
class Solution {
public:
int minStickers(vector<string>& stickers, string target) {
int m = stickers.size();
vector<vector<int>> mp(m, vector<int>(26, 0));
unordered_map<string, int> dp;
for (int i = 0; i < m; ++i) {
for (char c : stickers[i])
mp[i][c-'a']++;
}
dp[""] = 0;
return helper(dp, mp, target);
} private:
int helper(unordered_map<string, int>& dp, vector<vector<int>>& mp, string target) {
if (dp.count(target)) return dp[target];
int ans = INT_MAX, n = mp.size();
vector<int> tar(26, 0);
for (char c : target) tar[c-'a']++;
for (int i = 0; i < n; ++i) {
if (mp[i][target[0]-'a'] == 0) continue;
string s;
for (int j = 0; j < 26; ++j)
if (tar[j] - mp[i][j] > 0)
s += string(tar[j]-mp[i][j], 'a'+j);
int tmp = helper(dp, mp, s);
if (tmp != -1) ans = min(ans, 1+tmp);
}
dp[target] = ans == INT_MAX ? -1 : ans;
return dp[target];
}
};
Analysis:
https://leetcode.com/problems/stickers-to-spell-word/discuss/108318/C%2B%2BJavaPython-DP-%2B-Memoization-with-optimization-29-ms-(C%2B%2B)
691. Stickers to Spell Word的更多相关文章
- LeetCode 691. Stickers to Spell Word
原题链接在这里:https://leetcode.com/problems/stickers-to-spell-word/ 题目: We are given N different types of ...
- [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 ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- leetcode 学习心得 (4)
645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- leetcode hard
# Title Solution Acceptance Difficulty Frequency 4 Median of Two Sorted Arrays 27.2% Hard ...
随机推荐
- 网页静态化—redis | freemarker
1. 学习计划 1.商品详情页面展示,动态展示 jsp + redis 2.使用freemarker实现网页静态化 3.ActiveMq同步生成静态网页 两个方案对比,方案一依赖web容器,red ...
- 4600007972内销新单未取进FP
1.首先检查 in_sales_order表: select * from in_sales_order where so_id='04600007972'发现没有数据 2.接着检查从SAP导数的步骤 ...
- 编写DLL
想想还是把这个记录下吧,虽然不难,但由于平时写得不多,老是搞忘了. 1.我们来编写一个简单的DLL程序. 首先,我们来看下入口函数DllMain().DllMain()有3个参数: (1)hModul ...
- 【BZOJ2038】小Z的袜子【莫队】
题意 给出包含n个数字的序列,和m个查询.每次查询问区间[l,r]中挑选出两个数字,大小相同的概率为多少. 分析 莫队的入门题吧.代码是非常好写,关键是时间复杂度的证明.O(n*sqrt(n)).我还 ...
- 16进制颜色转普通RGB
做开发的会遇到很多时候UI给到的是16进制的颜色 然而很多时候我们需要把它转换成适合我们方法的RGB参数 当然通过网页转换也可以达到目的 但是我现在选择写一个分类来解决 一劳永逸~ 1.首先说下 ...
- Anaconda 安装和配置
Anaconda 安装和配置 1. Anaconda 安装 Anaconda说明及安装过程:Anaconda详细安装使用教程 2. Anaconda和Pip源修改 Anaconda源修改:打开Anac ...
- 使用UrlRewriteFilter对url进行更替
一般来说,使用struts之后url的访问实际上访问的是action的地址,为了不让该地址暴露给别人,可以采用UrlRewriteFilter来对url进行重写. 首先,在web.xml里面配置: & ...
- 实践作业4---DAY3阶段二。
第二阶段是用户调研,我们小组选择了一个5班的同学,作为采访对象.采访比较详实,但是样本太少,不太有说服力. 具体采访详情如下: 问:请问您使用喜欢发表Blog么? 答:肯定有啊. 问:都用什么网站发表 ...
- Go 语言并发笔记
前言: 本文是学习<<go语言程序设计>> -- 清华大学出版社(王鹏 编著) 的2014年1月第一版 做的一些笔记 , 如有侵权, 请告知笔者, 将在24小时内删除, 转载请 ...
- 新年奉献MVC+EF(CODEFIRST)+EASYUI医药MIS系统(转)
出处:http://www.cnblogs.com/chenlinzhi/p/4332628.html 本人闲来无事就把以前用Asp.net做过的一个医药管理信息系统用mvc,ef ,easyui重新 ...