In English, we have a concept called root, which can be followed by some other word to form another longer word - let's call this word successor. For example, when the root "an" is followed by the successor word "other", we can form a new word "another".\n\nGiven a dictionary consisting of many roots and a sentence consisting of words separated by spaces, replace all the successors in the sentence with the root forming it. If a successor can be replaced by more than one root, replace it with the root that has the shortest length.
Medium
Examples
Input:dictionary = ["cat","bat","rat"], sentence = "the cattle was rattled by the battery"
Output:"the cat was rat by the bat"
Approach 1
Level I: HashSet of Roots
Intuition
Store all roots in a HashSet. For each word in the sentence, check every possible prefix (starting from length 1) in the HashSet. Return the first match found.