Count Sentences From Anagram Swaps
Problem statement
Two words are anagrams when one can be made by rearranging all the letters of the other (a word is an anagram of itself).
You are given a list of different words words and a list of sentences. From a sentence you can make new sentences by replacing each of its words, independently, with any word from words that is an anagram of it (or keeping it).
For each sentence, return how many different sentences can be made this way, counting the sentence itself.
Function
countSentences(words: String[], sentences: String[]) → int[]Examples
Example 1
words = ["stop","pots","tops","a","net","ten"]sentences = ["stop a net","a a"]return = [6,1]In "stop a net", stop can be any of 3 words (stop, pots, tops), a only itself, and net 2 (net, ten): 3 × 1 × 2 = 6. In "a a" each word has 1 choice: 1.
Example 2
words = ["dusty","study","night","thing"]sentences = ["night study night"]return = [8]Each of the three words has 2 choices, chosen independently: 2 × 2 × 2 = 8.
Example 3
words = ["one"]sentences = ["one"]return = [1]One word with no other anagram: 1 sentence (itself).
Constraints
1 <= words.length <= 1000, the words are all different, and each has 1 to 20 lowercase English letters.1 <= sentences.length <= 100- Each sentence is 1 to 10 words separated by single spaces, and every word of every sentence appears in
words. - Every answer is at most
10^9.