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)

原文地址:https://www.cnblogs.com/ruruozhenhao/p/10354309.html

时间: 2024-11-10 07:15:28

691. Stickers to Spell Word的相关文章

LeetCode691. 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 sti

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

【LeetCode】动态规划(下篇)

[600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offers [639] Decode Ways II [646] Maximum Length of Pair Chain [647] Palindromic Substrings [650] 2 Keys Keyboard [651] 4 Keys Keyboard [656] Coin Path [6

【LeetCode】动态规划(上篇共75题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [5] Longest Palindromic Substring 给一个字符串,需要返回最长回文子串 解法:dp[i][j] 表示 s[i..j] 是否是回文串,转移方程是 dp[i][j] = 1 (if dp[i+1][j-1] = 1 && s[i] == s[j]),初始化条件是 if (s[i] == s[j] && (i == j

【LeetCode】动态规划(下篇共39题)

[600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offers [639] Decode Ways II [646] Maximum Length of Pair Chain [647] Palindromic Substrings [650] 2 Keys Keyboard [651] 4 Keys Keyboard [656] Coin Path [6

【LeetCode】回溯法 backtracking(共39题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [10]Regular Expression Matching [17]Letter Combinations of a Phone Number [22]Generate Parentheses (2019年2月13日) 给了一个N,生成N对括号的所有情况的字符串. n = 3 [ "((()))", "(()())", "(

Java操作Microsoft Word之jacob

转自: 现在我们一起来看看,用Java如何操作Microsoft Word. jacob,官网是http://danadler.com/jacob 这是一个开源的工具.最新版本1.7 官方的解释是:The JACOB Project: A JAva-COM Bridge 这是官方对下载文件的说明: jacob.jar: a JAR file for the java classes which you must add to your CLASSPATH. The package names r

POJ 1035 Spell checker

题目链接 http://poj.org/problem?id=1035 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24142   Accepted: 8794 Description You, as a member of a development team for a new spell checking program, are to write a module that will check the cor

The word is not correctly spelled问题解决

今天在做Ant/Xdoclet辅助生成hibernate实体映射文件时,eclipse出现了极其让我郁闷的错误,"The word is not correctly spelled" .然后我继续将代码执行下去,居然执行没有问题,还真的生成了实体映射文件.原来是eclipse的问题,打开eclipse,我忽略掉了错误提示,这才没有出现"The word is not correctly spelled"的错误提示. 操作步骤:在eclipse下的Window--Pr