0691. Stickers to Spell Word

https://leetcode.com/problems/stickers-to-spell-word

Description

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 <= 15

  • stickers[i] and target consist of lowercase English letters.

ac

class Solution {
    public int minStickers(String[] stickers, String target) {
        int n = stickers.length;
        int[][] map = new int[n][26];
        for (int i = 0; i < n; i++) {
            for (char c : stickers[i].toCharArray()) {
                map[i][c-'a']++;
            }
        }

        Map<String, Integer> memo = new HashMap<>();
        memo.put("", 0);

        return dfs(map, target, memo);
    }

    public int dfs(int[][] map, String target, Map<String, Integer> memo) {
        if (memo.containsKey(target)) return memo.get(target);

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

        int min = Integer.MAX_VALUE;
        for (int i = 0; i < map.length; i++) {
            if (map[i][target.charAt(0) - 'a'] == 0) continue; // critical optimization

            String newTarget = apply(map[i], t);
            if (newTarget.equals(target)) continue; // this sticker is useless, skip it
            int next = dfs(map, newTarget, memo);
            if (next != -1) min = Math.min(min, next + 1);
        }
        if (min == Integer.MAX_VALUE) min = -1;
        memo.put(target, min);
        return min;
    }

    public String apply(int[] cnts, int[] t) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 26; i++) {
            int remain = t[i] - cnts[i];
            while (remain-- > 0) {
                sb.append((char) ('a'+i));
            }
        }
        return sb.toString();
    }
}

/*
1) preprocess stickers, represent as char-cnt array; 2) DFS + memorization, dfs each time try to apply one sticker; 3) critical optimization: if (map[i][target.charAt(0) - 'a'] == 0) continue; that is, you may have many combination, but the one string in result must have contain target.charAt(0), just begin with this one.
*/

Last updated