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:

  1. ["with", "example", "science"], "thehat"

Output:

  1. 3

Explanation:

  1. We can use 2 "with" stickers, and 1 "example" sticker.
  2. After cutting and rearrange the letters of those stickers, we can form the target "thehat".
  3. Also, this is the minimum number of stickers necessary to form the target string.

Example 2:

Input:

  1. ["notice", "possible"], "basicbasic"

Output:

  1. -1

Explanation:

  1. 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++]

  1. class Solution {
  2. public:
  3. int minStickers(vector<string>& stickers, string target) {
  4. int m = stickers.size();
  5. vector<vector<int>> mp(m, vector<int>(26, 0));
  6. unordered_map<string, int> dp;
  7. for (int i = 0; i < m; ++i) {
  8. for (char c : stickers[i])
  9. mp[i][c-'a']++;
  10. }
  11. dp[""] = 0;
  12. return helper(dp, mp, target);
  13. }
  14.  
  15. private:
  16. int helper(unordered_map<string, int>& dp, vector<vector<int>>& mp, string target) {
  17. if (dp.count(target)) return dp[target];
  18. int ans = INT_MAX, n = mp.size();
  19. vector<int> tar(26, 0);
  20. for (char c : target) tar[c-'a']++;
  21. for (int i = 0; i < n; ++i) {
  22. if (mp[i][target[0]-'a'] == 0) continue;
  23. string s;
  24. for (int j = 0; j < 26; ++j)
  25. if (tar[j] - mp[i][j] > 0)
  26. s += string(tar[j]-mp[i][j], 'a'+j);
  27. int tmp = helper(dp, mp, s);
  28. if (tmp != -1) ans = min(ans, 1+tmp);
  29. }
  30. dp[target] = ans == INT_MAX ? -1 : ans;
  31. return dp[target];
  32. }
  33. };

  

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. sqlserver,oracle,mysql等的driver驱动,url怎么写

    oracle driver="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@localhost:1521 ...

  2. k8s v1.5.8 单节点搭建

    setsid etcd -name etcd -data-dir /var/lib/etcd -listen-client-urls http://0.0.0.0:2379,http://0.0.0. ...

  3. Tp3.1 文件上传到七牛云

    TP3.1 中不支持Composer 就无法用composer 安装 下载历史的SDK https://github.com/qiniu/php-sdk/releases/tag/v7.0.8 下载下 ...

  4. th:onclik()传参问题(前端使用了bootstrap)

    网上大多帖子是这么写的 onclick调javascript函数时,不能直接使用onclick=“editUser(${prod.id})”,这样会报错,需要修改成如下的格式. <a href= ...

  5. Redis只作为缓存,不做持久化的配置

    #1.配置缓存内存限制和清理策略 #作为缓存服务器,如果不加以限制内存的话,就很有可能出现将整台服务器内存都耗光的情况,可以在redis的配置文件里面设置: #example: # 限定最多使用1.5 ...

  6. EntityFramework - Code First - 数据迁移

    需求 在更新模型之后同步更新数据库里的表,并不丢失原有数据 使用默认值填充新增加的字段 EntityFramework迁移命令 Enable-Migrations 启用迁移 Add-Migration ...

  7. spark reduceByKey

    reduce(binary_function) reduce将RDD中元素前两个传给输入函数,产生一个新的return值,新产生的return值与RDD中下一个元素(第三个元素)组成两个元素,再被传给 ...

  8. 冲刺NOIP2015提高组复赛模拟试题(五)2.道路修建

    2.道路修建 描述 Description liouzhou_101最悲痛的回忆就是NOI2011的道路修建,当时开了系统堆栈,结果无限RE… 出于某种报复心理,就把那题神奇了一下: 在 Z星球上有N ...

  9. Mcrosoft中间语言的主要特征

    Mcrosoft中间语言显然在.NET FrameWork中起着非常重要的作用.现在讨论一下IL(Intermideate Language)的主要特征.因为面向.NET的所有语言在逻辑上都需要支持I ...

  10. Insufficient free space for journal files

    前两天请假了,公司的很多app突然挂掉了,说是mongodb莫名的挂掉了,赶紧进去看了看日志: --31T14:: [initandlisten] ERROR: Insufficient free s ...