01 · Question
You are given a list of equivalent string pairs synonyms where synonyms[i] = [s_i, t_i] indicates that s_i and t_i are equivalent strings. You are also given a sentence text.
Return all possible synonymous sentences sorted lexicographically.
02 · Analysis
Build synonym connected components first: equivalence is transitive, so a pair lookup alone is insufficient. At each word position choose any word in its component, keeping unchanged words as singleton choices. DFS enumerates the Cartesian product, and sorting the final sentences enforces lexicographic output. If S sentences of length at most L are produced, materialization costs O(SL) and sorting can cost O(SL log(S+1)), in addition to graph preprocessing.