426. Synonymous Sentences
Given a list of pairs of synonymous words and a sentence, return all possible sentences that can be formed by replacing words with their synonyms.
Example 1:
Input : synonyms = [["happy","joy"],["sad","sorrow"],["joy","cheerful"]],
text = "I am happy today but was sad yesterday"
Output :
["I am cheerful today but was sad yesterday",
"I am cheerful today but was sorrow yesterday",
"I am happy today but was sad yesterday",
"I am happy today but was sorrow yesterday",
"I am joy today but was sad yesterday",
"I am joy today but was sorrow yesterday"]
Explanation :
Using the given synonym relationships, we replace words with all possible synonymous alternatives and return the sentences in lexicographically sorted order.
Example 2:
Input : synonyms = [["happy","joy"],["cheerful","glad"]],
text = "I am happy today but was sad yesterday"
Output :
["I am happy today but was sad yesterday",
"I am joy today but was sad yesterday"]
Explanation :
The synonym pair ["cheerful", "glad"] is not connected to any word in the text, so only "happy" is replaced with "joy", generating two valid sentences in lexicographically sorted order.
Now Your Turn!
Pick the correct output for the given inputInput : synonyms = [["fast","quick"],["quick","speedy"],["smart","intelligent"]],
text = "He is fast and smart"
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 0 <= synonyms.length <= 10
- synonyms[i].length == 2
- 1 <= si.length, ti.length <= 10
- si != ti
- text consists of at most 10 words.
- All the pairs of synonyms are unique.
- The words of text are separated by single spaces.