Interview questions · Oracle
Count anagram-based replacements per phrase
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.
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
wordsmust stay unchanged. - If no word in the phrase has any anagram in
words(i.e., no replacement is possible), return0for that phrase.
Return a list of integers, one per phrase.
Example
Input:
phrases = ["hello world", "below elbow"]words = ["below", "elbow"]
Explanation:
- Phrase 1: neither
hellonorworldhas an anagram inwords→ no replacement possible →0 - Phrase 2:
belowandelboware anagrams, and both appear inwords. Each of the two positions can be replaced by eitherbeloworelbow→2 * 2 = 4
Output:
[0, 4]
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.
Community answers
Nothing here yet.
Join in on CSWork →