Replace Words - Problem

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 derivative. For example, when the root "help" is followed by the word "ful", we can form a derivative "helpful".

Given a dictionary consisting of many roots and a sentence consisting of words separated by spaces, replace all the derivatives in the sentence with the root forming it. If a derivative can be replaced by more than one root, replace it with the root that has the shortest length.

Return the sentence after the replacement.

Input & Output

Example 1 — Basic Root Replacement
$ Input: dictionary = ["cat","bat","rat"], sentence = "the cattle was rattled by the battery"
Output: "the cat was rat by the bat"
💡 Note: "cattle" has prefix "cat", "rattled" has prefix "rat", "battery" has prefix "bat"
Example 2 — Shortest Root Priority
$ Input: dictionary = ["a","aa","aaa"], sentence = "aadsfasf absbs bbab cadsfafs"
Output: "a a bbab cadsfafs"
💡 Note: "aadsfasf" and "absbs" both match "a" (shortest root), other words have no matching roots
Example 3 — No Matching Roots
$ Input: dictionary = ["catt","cat","bat","rat"], sentence = "the cattle was rattled by the battery"
Output: "the cat was rat by the bat"
💡 Note: Even with "catt" in dictionary, "cat" is chosen as it's shorter and still matches "cattle"

Constraints

  • 1 ≤ dictionary.length ≤ 1000
  • 1 ≤ dictionary[i].length ≤ 100
  • 1 ≤ sentence.length ≤ 106
  • dictionary[i] and sentence consist of only lower-case letters

Visualization

Tap to expand
Replace Words - Hash Set Optimization INPUT Dictionary (Roots): "cat" "bat" "rat" Hash Set: {"cat", "bat", "rat"} O(1) lookup time Sentence: "the cattle was rattled by the battery" Words to check: cattle rattled battery ALGORITHM STEPS 1 Build Hash Set Store all roots in set 2 Split Sentence Get individual words 3 Check Prefixes Find shortest root match 4 Replace Words Substitute with root Prefix Matching: "cattle" --> "c","ca","cat" OK "rattled" --> "r","ra","rat" OK "battery" --> "b","ba","bat" OK "the" --> "t","th","the" NO (keep original if no match) FINAL RESULT Replaced Sentence: "the cat was rat by the bat" Replacements Made: cattle --> cat rattled --> rat battery --> bat the, was, by unchanged Output: "the cat was rat by the bat" Key Insight: Using a Hash Set allows O(1) lookup for each prefix check. For each word, we check all prefixes from length 1 to word length, returning the shortest matching root. This optimizes the brute-force approach from O(n*m*k) to O(n*L) where L is maximum word length, making prefix matching efficient. TutorialsPoint - Replace Words | Hash Set Optimization Approach
Asked in
Google 25 Amazon 18 Microsoft 15 Facebook 12
28.5K Views
Medium Frequency
~25 min Avg. Time
845 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen