Generate all synonymous sentences from equivalent word pairs in sorted order

Quick Overview

Given pairs of equivalent words and a sentence, return every sentence you can form by swapping words for their equivalents, in sorted order. It tests grouping words linked through chains of pairs into equivalence classes and enumerating every combination of choices across the sentence.

Generate all synonymous sentences from equivalent word pairs in sorted order

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a list of word pairs `synonyms`, where `synonyms[i] = [s_i, t_i]` means `s_i` and `t_i` are equivalent words, and a sentence `text`. A synonymous sentence is any sentence you can get from `text` by replacing some of its words (possibly none) with words equivalent to them. Return all possible synonymous sentences, sorted lexicographically. ### Function Signature ```python def generate_sentences(synonyms: list[list[str]], text: str) -> list[str]: ``` ### Rules - Equivalence is symmetric and transitive. If `"happy"` is paired with `"joy"` and `"joy"` is paired with `"cheerful"`, all three words are interchangeable, even though `"happy"` and `"cheerful"` never appear in the same pair. - Each word position in `text` is replaced independently, so a word that appears twice can take a different equivalent at each position. - Words are matched whole and exactly. A word that appears in no pair stays as it is, and a pair containing `"a"` does not affect the word `"am"`. - The original sentence is one of the synonymous sentences. - Sort the result with ordinary string comparison (the order Python's `sorted` produces). ### Constraints - `0 <= len(synonyms) <= 10` - `len(synonyms[i]) == 2` - `1 <= len(s_i), len(t_i) <= 10` - `s_i` and `t_i` consist of lowercase English letters. - `1 <= len(text) <= 100` - `text` consists of English letters and spaces. - The words in `text` are separated by single spaces. ### Examples ```text 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"] ``` "happy" is linked to "cheerful" through "joy", so it belongs to the group {cheerful, happy, joy}, and "sad" belongs to {sad, sorrow}. Three choices at one position and two at the other give 6 sentences. ```text Input: synonyms = [["a","b"],["c","d"],["e","f"]], text = "a c e" Output: ["a c e","a c f","a d e","a d f","b c e","b c f","b d e","b d f"] ``` The three words come from three separate groups of size 2, so there are 8 sentences. ```text Input: synonyms = [["big","large"]], text = "big big" Output: ["big big","big large","large big","large large"] ``` Both positions vary independently, so the same word can become "big" at one position and "large" at the other.

Overview: Given pairs of equivalent words and a sentence, return every sentence you can form by swapping words for their equivalents, in sorted order. It tests grouping words linked through chains of pairs into equivalence classes and enumerating every combination of choices across the sentence.

Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a list of word pairs synonyms, where synonyms[i] = [s_i, t_i] means s_i and t_i are equivalent words, and a sentence text. A synonymous sentence is any sentence you can get from text by replacing some of its words (possibly none) with words equivalent to them.

Return all possible synonymous sentences, sorted lexicographically.

Function Signature

def generate_sentences(synonyms: list[list[str]], text: str) -> list[str]:

Rules

  • Equivalence is symmetric and transitive. If "happy" is paired with "joy" and "joy" is paired with "cheerful" , all three words are interchangeable, even though "happy" and "cheerful" never appear in the same pair.
  • Each word position in text is replaced independently, so a word that appears twice can take a different equivalent at each position.
  • Words are matched whole and exactly. A word that appears in no pair stays as it is, and a pair containing "a" does not affect the word "am" .
  • The original sentence is one of the synonymous sentences.
  • Sort the result with ordinary string comparison (the order Python's sorted produces).

Constraints

  • 0 <= len(synonyms) <= 10
  • len(synonyms[i]) == 2
  • 1 <= len(s_i), len(t_i) <= 10
  • s_i and t_i consist of lowercase English letters.
  • 1 <= len(text) <= 100
  • text consists of English letters and spaces.
  • The words in text are separated by single spaces.

Examples

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"]

"happy" is linked to "cheerful" through "joy", so it belongs to the group {cheerful, happy, joy}, and "sad" belongs to {sad, sorrow}. Three choices at one position and two at the other give 6 sentences.

Input: synonyms = [["a","b"],["c","d"],["e","f"]], text = "a c e"
Output: ["a c e","a c f","a d e","a d f","b c e","b c f","b d e","b d f"]

The three words come from three separate groups of size 2, so there are 8 sentences.

Input: synonyms = [["big","large"]], text = "big big"
Output: ["big big","big large","large big","large large"]

Both positions vary independently, so the same word can become "big" at one position and "large" at the other.