JIAKAOBO

LeetCode

venmo
wechat

感谢赞助!

  • ㊗️
  • 大家
  • offer
  • 多多!

Problem

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 string target 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.

Return the minimum number of stickers that you need to spell out target. If the task is impossible, return -1.

Note: In all test cases, all words were chosen randomly from the 1000 most common US English words, and target was chosen as a concatenation of two random words.

Example 1:

Input: stickers = ["with","example","science"], target = "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: stickers = ["notice","possible"], target = "basicbasic"
Output: -1
Explanation:
We cannot form the target "basicbasic" from cutting letters from the given stickers.

Constraints:

  • n == stickers.length
  • 1 <= n <= 50
  • 1 <= stickers[i].length <= 10
  • 1 <= target.length <= 15
  • stickers[i] and target consist of lowercase English letters.

Code

class Solution {
    // dp[s] is the minimum stickers required for string s
    public int minStickers(String[] stickers, String target) {
        int[][] mp = new int[stickers.length][26];
        Map<String, Integer> dp = new HashMap<>();

        // 统计每个sticker字母
        for (int i = 0; i < stickers.length; i++) {
            for (char c : stickers[i].toCharArray()) {
                mp[i][c - 'a']++;
            }
        }

        dp.put("", 0);
        return helper(dp, mp, target);
    }

    private int helper(Map<String, Integer> dp, int[][] mp, String target) {
        if (dp.containsKey(target)) {
            return dp.get(target);
        }

        int res = Integer.MAX_VALUE;
        int[] tar = new int[26];

        for (char c : target.toCharArray()) {
            tar[c - 'a']++;
        }

        // try every sticker
        for (int i = 0; i < mp.length; i++) {
            // 这个sticker没有target的第一个字母
            if (mp[i][target.charAt(0) - 'a'] == 0)
                continue;

            // 这个sticker有target的第一个字母
            StringBuilder sb = new StringBuilder();

            // apply a sticker on every character of target
            for (int j = 0; j < 26; j++) {
                // target中有这个字符
                if (tar[j] > 0) {
                    // 使用sticker之后还差几个字符
                    int remain = tar[j] - mp[i][j];
                    if (remain > 0) {
                        for (int k = 0; k < remain; k++) {
                            sb.append((char) ('a' + j));
                        }
                    }
                }
            }

            // 剩下的还需要匹配的target
            String s = sb.toString();
            int tmp = helper(dp, mp, s);

            if (tmp != -1) {
                res = Math.min(res, 1 + tmp);
            }
        }

        dp.put(target, res == Integer.MAX_VALUE ? -1 : res);

        return dp.get(target);
    }
}
  • time: O((m * n * 2^n), n - length of target, m - count of stickers
  • space: O(2^n)