|
| 1 | +package dynamic_programming; |
| 2 | + |
| 3 | +/** |
| 4 | + * Created by gouthamvidyapradhan on 14/07/2019 We are given N different types of stickers. Each |
| 5 | + * sticker has a lowercase English word on it. |
| 6 | + * |
| 7 | + * <p>You would like to spell out the given target string by cutting individual letters from your |
| 8 | + * collection of stickers and rearranging them. |
| 9 | + * |
| 10 | + * <p>You can use each sticker more than once if you want, and you have infinite quantities of each |
| 11 | + * sticker. |
| 12 | + * |
| 13 | + * <p>What is the minimum number of stickers that you need to spell out the target? If the task is |
| 14 | + * impossible, return -1. |
| 15 | + * |
| 16 | + * <p>Example 1: |
| 17 | + * |
| 18 | + * <p>Input: |
| 19 | + * |
| 20 | + * <p>["with", "example", "science"], "thehat" Output: |
| 21 | + * |
| 22 | + * <p>3 Explanation: |
| 23 | + * |
| 24 | + * <p>We can use 2 "with" stickers, and 1 "example" sticker. After cutting and rearrange the letters |
| 25 | + * of those stickers, we can form the target "thehat". Also, this is the minimum number of stickers |
| 26 | + * necessary to form the target string. Example 2: |
| 27 | + * |
| 28 | + * <p>Input: |
| 29 | + * |
| 30 | + * <p>["notice", "possible"], "basicbasic" Output: |
| 31 | + * |
| 32 | + * <p>-1 Explanation: |
| 33 | + * |
| 34 | + * <p>We can't form the target "basicbasic" from cutting letters from the given stickers. Note: |
| 35 | + * |
| 36 | + * <p>stickers has length in the range [1, 50]. stickers consists of lowercase English words |
| 37 | + * (without apostrophes). target has length in the range [1, 15], and consists of lowercase English |
| 38 | + * letters. In all test cases, all words were chosen randomly from the 1000 most common US English |
| 39 | + * words, and the target was chosen as a concatenation of two random words. The time limit may be |
| 40 | + * more challenging than usual. It is expected that a 50 sticker test case can be solved within 35ms |
| 41 | + * on average. |
| 42 | + * |
| 43 | + * <p>Solution: O(2 ^ T x T x S) where T is the length of target and S is length of sticker array. |
| 44 | + * Each state is a combination of characters selected in the target sticker plus the total count of |
| 45 | + * stickers used. Cache the minimum count in each state and explore all the different possible |
| 46 | + * states. |
| 47 | + */ |
| 48 | +public class StickersToSpellWord { |
| 49 | + /** |
| 50 | + * Main method |
| 51 | + * |
| 52 | + * @param args |
| 53 | + */ |
| 54 | + public static void main(String[] args) { |
| 55 | + String[] stickers = {"bright", "neighbor", "capital"}; |
| 56 | + |
| 57 | + System.out.println(new StickersToSpellWord().minStickers(stickers, "originalchair")); |
| 58 | + } |
| 59 | + |
| 60 | + private int destination = 0; |
| 61 | + private int min = Integer.MAX_VALUE; |
| 62 | + private int[][] DP; |
| 63 | + |
| 64 | + public int minStickers(String[] stickers, String target) { |
| 65 | + for (int i = 0; i < target.length(); i++) { |
| 66 | + destination |= (1 << i); |
| 67 | + } |
| 68 | + DP = new int[destination][target.length() + 1]; |
| 69 | + int answer = dp(stickers, target, 0, 0); |
| 70 | + return answer == Integer.MAX_VALUE ? -1 : answer; |
| 71 | + } |
| 72 | + |
| 73 | + private int dp(String[] stickers, String target, int curr, int count) { |
| 74 | + if (curr == destination) { |
| 75 | + return count; |
| 76 | + } else { |
| 77 | + if (count > min) return Integer.MAX_VALUE; |
| 78 | + if (DP[curr][count] != 0) return DP[curr][count]; |
| 79 | + DP[curr][count] = Integer.MAX_VALUE; |
| 80 | + for (String s : stickers) { |
| 81 | + int temp = 0; |
| 82 | + char[] arr = s.toCharArray(); |
| 83 | + for (int i = 0, l = target.length(); i < l; i++) { |
| 84 | + if ((curr & (1 << i)) == 0) { |
| 85 | + char targetChar = target.charAt(i); |
| 86 | + for (int j = 0; j < arr.length; j++) { |
| 87 | + if (arr[j] == targetChar) { |
| 88 | + arr[j] = '0'; |
| 89 | + temp |= (1 << i); |
| 90 | + break; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + if (temp > 0) { |
| 96 | + int child = (curr | temp); |
| 97 | + int retValue = dp(stickers, target, child, count + 1); |
| 98 | + DP[curr][count] = Math.min(DP[curr][count], retValue); |
| 99 | + min = Math.min(min, DP[curr][count]); |
| 100 | + } |
| 101 | + } |
| 102 | + return DP[curr][count]; |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments