133. Replace Words

Given a dictionary of roots dictionary and a sentence sentence, replace each word in the sentence with the shortest root from the dictionary. If there is no root, leave the word unchanged.
Example 1
Input: dictionary = ["cat","bat","rat"], sentence = "the cattle was rattled by the battery"
Output: "the cat was rat by the bat"
Explanation: The roots cat, rat, bat replace the words cattle, rattled, battery.
Example 2
Input: dictionary = ["a","b","c"], sentence = "aadsfasf absbs bbab cadsfafs"
Output: "a a b c"
Explanation: Single-letter roots replace word prefixes.
mediumstringstrie
JavaScript
Loading...
Line 1, Char 1