Data Structures19 sections · 729 units
Open in Course

Problem - Replace Words

Trie for dictionary lookup

Given a dictionary of root words and a sentence, replace each word in the sentence with its shortest root (if one exists). Example: dictionary = ["cat", "bat", "rat"], sentence = "the cattle was rattled by the battery" Output: "the cat was rat by the bat" "cattle" → "cat" (shortest root prefix) "rattled" → "rat" "battery" → "bat" Constraints: dictionary has up to 10001000 roots, sentence has up to 10001000 words.