Monday, October 16, 2017

[leetCode] 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.
Code:
 String toKey(int[] arr){
        StringBuilder sb=new StringBuilder();
        boolean allZ=true;
        for (int i=0;i<26;++i){
            int n=arr[i];
            sb.append(n).append(",");
            if (n>0)allZ=false;
        }
        if (allZ)return "";
        return sb.toString();
    }
    public int minStickers(String[] stickers, String target) {
        if (target.isEmpty())return 0;
        int ns=stickers.length;
        int tl=target.length();
        int[][] letters=new int[ns][26];
        for (int i=0;i<stickers.length;++i){
            String s=stickers[i];
            for (char c:s.toCharArray())++letters[i][c-'a'];
        }
        int[] targetLetters=new int[27];
        for (char c:target.toCharArray())++targetLetters[c-'a'];
        targetLetters[26]=0;
        String key=toKey(targetLetters);
        int ans=0;
        if (key.isEmpty())return 0;
        Queue<int[]>q=new LinkedList<>();
        Set<String> seenKey= new HashSet<>();
        seenKey.add(key);
        q.add(targetLetters);
        while (!q.isEmpty()){
            int[]cur=q.remove();
            for (int i=0;i<ns;++i){
                int[] next=cur.clone();
                int[]letter=letters[i];
                for (int j=0;j<26;++j){
                    if (letter[j]>=0)
                        next[j]=Math.max(next[j]-letter[j], 0);
                }
                ++next[26];
                String nextKey=toKey(next);
                if (nextKey.isEmpty())return next[26];
                if (seenKey.add(nextKey)){
//                    log.info("add key: {}", nextKey);
                    q.add(next);
                }
            }
        }
        return -1;
    }

No comments:

Post a Comment