Skip to content

691. Stickers To Spell Word

Difficulty: Hard

LeetCode Problem View on GitHub


691. Stickers to Spell Word

Hard


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.

Solution

class Solution {
    private HashMap<String, Integer> dp;
    public int minStickers(String[] stickers, String target) {
        int n = stickers.length;
        dp = new HashMap<>();
        dp.put("", 0);
        int ans = solve(target, stickers);
        return ans == Integer.MAX_VALUE / 10 ? -1 : ans;
    }

    private int solve(String target, String stickers[]) {
        if (dp.containsKey(target)) 
            return dp.get(target);

        int currFreq[] = new int[26];
        for (int i = 0; i < target.length(); i++)
            currFreq[target.charAt(i) - 'a']++;

        int ans = Integer.MAX_VALUE / 10;

        for (int i = 0; i < stickers.length; i++) {
            String current = stickers[i];
            boolean willHelp = false;
            int thisFreq[] = new int[26];
            for (int j = 0; j < current.length(); j++) {
                if (currFreq[current.charAt(j) - 'a'] > 0) 
                    willHelp = true;
                thisFreq[current.charAt(j) - 'a']++;
            }
            if (willHelp == false) 
                continue;
            String newString = "";
            for (int j = 0; j < target.length(); j++) {
                if (thisFreq[target.charAt(j) - 'a'] > 0) 
                    thisFreq[target.charAt(j) - 'a']--;
                else 
                    newString += target.charAt(j);
            }
            ans = Math.min(ans, 1 + solve(newString, stickers));
        }
        dp.put(target, ans);
        return ans;
    }
}

Complexity Analysis

  • Time Complexity: O(?)
  • Space Complexity: O(?)

Approach

Detailed explanation of the approach will be added here