1568. Letter Kit Queries
A craft shop owns a list of words, and each customer brings a kit of letter tiles, given as a string in kits (repeated letters in a kit do not matter; only which letters it contains). A word can be spelled from a kit when every distinct letter of the word appears in the kit; each letter may be used as often as needed.
For each kit, in the order given, count how many entries of words can be spelled from it. Equal words count separately. Return the counts as an int array with one entry per kit.
Hint: represent each word by the set of its letters as a bitmask, count the masks in a hash map, and for each kit (at most 7 distinct letters, so at most 128 subsets) add up the counts of all its submasks.
Required complexity: O(total word length + kits * 2^7) time, O(distinct masks) space.
Example 1
- Input:
- words = ["ab","ba","abc","c","dd"]kits = ["ab","abc","d"]
- Output:
- [2,4,1]
- Explanation:
Kit ab spells ab and ba, kit abc spells ab, ba, abc and c, and kit d spells only dd, giving [2,4,1].
Example 2
- Input:
- words = ["xyz"], kits = ["xy","zyx"]
- Output:
- [0,1]
- Explanation:
Kit xy lacks the letter z, while kit zyx contains all three letters, so the answer is [0,1].
Constraints
1 ≤ words.length ≤ 105
1 ≤ words[i].length ≤ 20
1 ≤ kits.length ≤ 104
1 ≤ kits[j].length ≤ 7
All strings consist of lowercase English letters.
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 4,000 msC++ 1,000 msJava 2,000 msJavaScript 2,000 msTypeScript 2,000 ms
Expected complexity
- Time
- O(L + K * 2^7)
- Space
- O(W)