Synonymous Sentences

Moderate
0/80
Average time to solve is 20m
profile
Contributed by
2 upvotes
Asked in company
Flipkart limited

Problem statement

You are given a sentence consisting of several space-separated words and a list of pairs of synonyms. Your task is to find out all possible sentences that can be formed by replacing any word in the sentence with any of its synonyms.

Note :

1. A word may or may not have synonyms.
2. There can be multiple synonyms for a single word.
3. All words only contain lowercase English letters.
Detailed explanation ( Input/output format, Notes, Images )
Input Format :
The first line contains an integer T, which denotes the number of test cases to be run. Then, the T test cases follow. 

The first line of each test case contains two space-separated integers, N and M, denoting the number of pairs of synonyms in the list and the number of words in the sentence.

The second line contains 2*N space-separated words. For every, i from 1 to N, (2*i-1)-th and (2*i)-th words are synonyms of each other.

The third line contains M space-separated words which form the sentence.
Output Format :
For each test case, print in separate lines all sentences that can be formed by replacing one or more words with their synonyms. 

Please note that all the sentences must be printed in lexicographically sorted order.

The output of every test case will be printed in a separate line.
Note :
You do not need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 10
1 <= N,M <= 8
1 <= |word| <= 10

Where |word| denotes the length of any word that appears in the sentence.

Time Limit: 1sec
Sample Input 1 :
1
2 5
bright pleasant happy bright
today is a pleasant day
Sample Output 1 :
today is a bright day
today is a happy day
today is a pleasant day
Explanation For Sample Input 1 :
In the first test case, we can see that “bright” is a synonym of “pleasant”. We can also see that “happy” is also a synonym of “bright”. This means that “bright”, “pleasant” and “happy” are all synonyms. Hence we can form three different sentences using these words one by one. 
Hint

Try to find all groups of synonyms.

Approaches (1)
Using Recursion and Hashing

The approach is to use hashing and recursion to find out different groups of words so that every word of a group is a synonym of every other word in that particular group. Now, replace every possible word with their respective synonyms one by one using recursion and when all the words for this traversal have been replaced, push this sentence into the vector and continue traversing to find all other possible sentences.

Steps:

  1. Initialize a vector, allSentences to store all sentences. Each sentence will be stored in the form of a vector of strings.
  2. Initialize a hashmap, unordered_map<string, set<string>> groups that will be used to find groups of all the words that are synonyms of each other. Each word will be matched with a set of words. Each word in this set can be used in place of the original word.
  3. Run a loop for i=0 to i=synonyms.size() and do:
    1. Insert second word into the set of the first word i.e. groups[i][0].insert(groups[i][1]).
    2. Insert first word into the set of the second word i.e. groups[i][1].insert(groups[i][0]).
  4. Now, run a loop for i=0 to i=sentence.size() and do:
    1. Declare word = sentence[i].
    2. Create a set, wordList, that will store all the synonyms for this word.
    3. Call the function findSynonyms(word, wordList, groups). This will store all the synonyms of the current word.
    4. Now, make groups[word] = wordList.
  5. Now, call the function useSynonymn(sentence, groups, allSentences, 0). This function will traverse through every index, and one by one, replace all the synonyms for the word present at the current index. When the current sentence is completed, the sentence will be pushed into the vector, allSentences.
  6. Return the vector, allSentences.

void findSynonyms(string word, set<string>& wordList, unordered_map<string, set<string>>& groups) 

  1. Insert word in the wordList.
  2. If word is not present in the hashmap, groups, simply return from here.
  3. Create a set temp and make temp = group[words]
  4. Now, erase word from the hashmap.
  5. For each string text in temp, call findSynonyms(text, wordList, groups).
  6. Now, again make groups[word] = temp.

void useSynonym(vector<string> currentSentence, unordered_map<string, set<string>>& groups,

                vector<vector<string>>& allSentences, int idx) 

  1. If all words for the current sentence have been replaced i.e if idx == sentence.size(),
    1. allSentences.push_back(currentSentence).
    2. Return from here.
  2. For each string synonym in groups[sentence[idx]],
    1. Replace the word at current index by the synonym i.e make currentSentence[idx] = synonym.
    2. Call useSynonym(currentSentence, groups, allSentences, idx+1).
Time Complexity

O(N^M), where N is the number of pairs of synonyms, and M is the number of words in the sentence.

 

In the worst case, all words in the list of synonyms can be synonyms of a single word. So, that word can have N synonyms. If all the words in the sentence are synonyms of each other, we can replace each word with its N synonyms, and since there are M words in the sentence, the time complexity becomes O(N^M).

Space Complexity

O(N^M), where N is the number of pairs of synonyms, and M is the number of words in the sentence.

 

We need extra space to maintain hashmaps to store the synonyms for every word of the sentence. Since there can be N different synonyms for each word, we need extra O(N^M) space.

Code Solution
(100% EXP penalty)
Synonymous Sentences
Full screen
Console