Medium

Quiz

#648 Replace Words

APPROACH

Given a dictionary of root words and a sentence, replace every word in the sentence that begins with a dictionary root with the shortest matching root. Words that have no root prefix are left unchanged. Return the modified sentence.

Example 1:

Input: dictionary = ["cat","bat","rat"], sentence = "the cattle was rattled by the battery"
Output: "the cat was rat by the bat"

Example 2:

Input: dictionary = ["a","b","c"], sentence = "aadsfasf absbs bbab cadsfafs"
Output: "a a b c"
1 of 4
1:00

What is the optimal approach for this problem?