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的更多相关文章

  1. LeetCode 691. Stickers to Spell Word

    原题链接在这里:https://leetcode.com/problems/stickers-to-spell-word/ 题目: We are given N different types of ...

  2. [LeetCode] Stickers to Spell Word 贴片拼单词

    We are given N different types of stickers. Each sticker has a lowercase English word on it. You wou ...

  3. [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 ...

  4. LeetCode691. Stickers to Spell Word

    We are given N different types of stickers. Each sticker has a lowercase English word on it. You wou ...

  5. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

  6. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  7. leetcode 学习心得 (4)

    645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...

  8. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  9. leetcode hard

    # Title Solution Acceptance Difficulty Frequency     4 Median of Two Sorted Arrays       27.2% Hard ...

随机推荐

  1. 【原】Coursera—Andrew Ng机器学习—Week 7 习题—支持向量机SVM

    [1] [2] Answer: B. 即 x1=3这条垂直线. [3] Answer: B 因为要尽可能小.对B,右侧红叉,有1/2 * 2  = 1 ≥ 1,左侧圆圈,有1/2 * -2  = -1 ...

  2. 数组和集合(四)、Map集合的使用总结

    一.概述 键值对,无序 键唯一.值不唯一 只允许存在一个Key为null元素 二.实现类 1. HashMap · 无序,数组+链表+红黑树 · 非线程安全 2. LinkedHashMap · 有序 ...

  3. curl 访问 k8s

    curl https://mhc:6443/api --cacert ssl/ca.crt --key client_ssl/cs_client.key --cert client_ssl/cs_cl ...

  4. 奶牛易物-Alpha版本测试报告

    1.在测试过程中总共发现了多少Bug?每个类别的Bug分别为多少个? a. 修复的bug: 1.mapper接口与mapper.xml文件绑定的问题; 2..配置逆向工程的配置文件的问题; 3.在编码 ...

  5. H5(1)

    css布局模型 清楚了CSS 盒模型的基本概念. 盒模型类型, 我们就可以深入探讨网页布局的基本模型了.布局模型与盒模型一样都是 CSS 最基本. 最核心的概念. 但布局模型是建立在盒模型基础之上,又 ...

  6. 5-SOM神经网络

    https://blog.csdn.net/gavinlyy/article/details/8302814 https://blog.csdn.net/u011314012/article/deta ...

  7. cacti-不出图形,cacti.log中出“ERROR: SQL Assoc Failed!

    [root@CactiEZ log]# tail cacti.log 2016年04月06日 14:53:16 PM - CMDPHP: Poller[0] ERROR: SQL Cell Faile ...

  8. C++编程语言学习资料

    C++ How to Program, 7/e (C++大学教程 第7版) 英文原版 全彩页 C++大学教程(第五版)中文版高清PDF下载 C++大学教程 第五版 (C++ How to Progra ...

  9. Luogu 4245 【模板】任意模数NTT

    这个题还有一些其他的做法,以后再补,先记一下三模数$NTT$的方法. 发现这个题不取模最大的答案不会超过$10^5 \times 10^9 \times 10^9 = 10^{23}$,也就是说我们可 ...

  10. 掌握所有IO口的外部中断

    外部中断配置流程 1.初始化IO口工作在普通IO.上拉输入状态. 2.首先开IO口组中断(P0IE=1.P1IE=1.P2IE=1): 3.开组内对应的具体某IO口中断(P0IEN.P1IEN.P2I ...