Problem
You are given:
-
phrases
: a list of strings, where each string is a phrase containing words separated by single spaces.
-
words
: a list of strings.
A word a can be replaced by a word b if a and b are anagrams (they contain the same letters with the same frequencies; order does not matter).
For each phrase, compute how many different phrases can be formed by replacing each replaceable word in the phrase with any anagram from words.
-
Words in the phrase that have
no anagram in
words
must stay unchanged.
-
If
no word
in the phrase has any anagram in
words
(i.e., no replacement is possible), return
0
for that phrase.
Return a list of integers, one per phrase.
Example
Input:
-
phrases = ["hello world", "below elbow"]
-
words = ["below", "elbow"]
Explanation:
-
Phrase 1: neither
hello
nor
world
has an anagram in
words
→ no replacement possible →
0
-
Phrase 2:
below
and
elbow
are anagrams, and both appear in
words
. Each of the two positions can be replaced by either
below
or
elbow
→
2 * 2 = 4
Output:
Requirements / Notes
-
Treat anagrams case-sensitively or case-insensitively consistently (state your choice in implementation; a common assumption is lowercase inputs).
-
You may assume phrases contain only alphabetic words and spaces.
-
The result may exceed 32-bit integer range; use 64-bit if needed.