2Brute2Best
🔥0

Replace Words

Medium·LeetCode #648 Pro OnlyMedium
LeetCode
01

Problem Statement

You get a list of roots and a sentence. A word in the sentence is a derivative of a root when the word starts with that root. For example, cattle starts with the root cat.

Replace every derivative with its root. If more than one root fits a word, use the shortest one. A word with no matching root stays as it is.

Return the new sentence. Words are separated by single spaces, with no space at the start or the end.

Input

dictionary, a list of root words, and sentence, a list of words separated by single spaces.

Output

The sentence after the replacements.

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

cattle starts with cat, so it becomes cat. rattled starts with rat, and battery starts with bat. the, was and by do not start with any root, so they stay.

Example 2
Input: dictionary = ["a", "b", "c"], sentence = "aadsfasf absbs bbab cadsfafs"
Output: "a a b c"

Each word starts with one of the roots a, b or c, so each word is cut down to that single letter.

Example 3
Input: dictionary = ["cat", "cattle"], sentence = "cattle catalog"
Output: "cat cat"

Both roots fit cattle, so the shorter root cat wins. catalog fits only cat.

Constraints
  • • 1 ≤ dictionary.length ≤ 1000
  • • 1 ≤ dictionary[i].length ≤ 100
  • • dictionary[i] uses only lowercase letters
  • • 1 ≤ sentence.length ≤ 10^6
  • • sentence uses only lowercase letters and spaces
  • • The sentence has between 1 and 1000 words, and each word has length 1 to 1000
  • • Two neighbouring words are separated by exactly one space, with no space at the start or the end
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“root at the start of a word”
→ Each word is asked which stored string it starts with, so the roots go into a trie and each word is walked from its first letter.
“shortest root”
→ On a walk from the start, the first match is the shortest one, so the walk stops at the first end flag.
“no root fits”
→ A walk that falls off means no match, so the word stays the same.
03

Approach Complexity Ladder

Brute Force — try every root for every wordO(W · R · M)O(S)

For each word, compare with every root and keep the shortest one the word starts with

Better — hash set of roots, try each prefixO(W · M²)O(D)

Put the roots in a hash set; for each word, look up its prefixes from shortest to longest

Optimal — trie of roots, stop at the first end flagO(D + S)O(26 · D)

Insert the roots into a trie; walk each word once and stop at the first end flag

Brute Force Intuition

For each word in the sentence, look at every root. A root fits when the word starts with it. Among the roots that fit, keep the shortest. If no root fits, keep the word.

Recognition rule for this sub-pattern:

Insert each word letter by letter; search by walking the same path.
Remember this · Trie
Trigger
Many words + prefix search, autocomplete, word dictionary with wildcards, maximum XOR.
Think
Do many queries share prefixes that I keep re-checking?
Key question
What does each node store (children, end-of-word, count)?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Trie translate into production architectures and low-level memory mechanics.