This question evaluates skills in string processing, anagram grouping, frequency mapping, and combinatorial counting to determine how many phrase variations arise from permissible replacements in the Coding & Algorithms domain.
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
must stay unchanged.
words
(i.e., no replacement is possible), return
0
for that phrase.
Return a list of integers, one per phrase.
Input:
phrases = ["hello world", "below elbow"]
words = ["below", "elbow"]
Explanation:
hello
nor
world
has an anagram in
words
→ no replacement possible →
0
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:
[0, 4]