Skip to content
Problem 648 · LeetCode 648(opens in a new tab)

Replace Words

MediumHash tables

You get a list of short words called roots, dictionary, and a sentence of words separated by single spaces. A word is built on a root when the word starts with that root, for example cart starts with car.

Replace each word in the sentence by the root it starts with. If several roots fit, use the shortest one. A word that starts with no root stays as it is. Return the new sentence.

Example 1

Input:  dictionary = ["car", "ca", "dog"],
        sentence = "cart dogma cab do"
Output: "ca dog ca do"
Why:    "cart" fits both "car" and "ca"; the shorter wins
2 more examples and the constraints

Example 2

Input:  dictionary = ["x", "xy"], sentence = "xyz xx yx"
Output: "x x yx"
Why:    "yx" does not start with any root

Example 3

Input:  dictionary = ["abc"], sentence = "ab abc abcd"
Output: "ab abc abc"
Why:    "ab" is shorter than the root, so it stays

Constraints

  • 1 ≤ number of roots ≤ 1,000, each 1 to 100 lower-case letters
  • 1 ≤ words in the sentence ≤ 1,000, each 1 to 1,000 lower-case letters
  • The sentence is at most 1,000,000 characters, with single spaces between words

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Check every rootO(W·D·L)
  2. Set of roots, prefixes shortest firstO(W·L²)
  3. Trie of the rootsO(n)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for 1,000 roots and 1,000 wordsVerdict
Check every rootO(W·D·L)
Mention it, then move on
Set of rootsO(W·L²)
Passes; builds many strings
Trie of the rootsO(n)
Best; each letter read once

W = words in the sentence, D = roots, L = length of a root or word, n = total input length.

Edge cases to test

  • Several roots fit. car and ca both fit cart; the shorter one must win, whatever order the roots are listed in.
  • Word shorter than every root. ab with root abc stays ab.
  • Word equals a root. abc with root abc stays abc.
  • Root inside a word. bell contains ell but does not start with it, so it stays.

Common mistakes

  • Taking the first root in the dictionary that fits instead of the shortest one.
  • Forgetting to stop at the first end mark in the trie, so cart becomes car.
  • Checking whether the root appears anywhere in the word instead of at the start.

How to talk through it

  1. 1Clarify. Several roots may fit: shortest wins? Is a word with no root left alone? Single spaces only?
  2. 2Give the brute force. Compare every word with every root; say it is O(W·D·L).
  3. 3Improve with a set. Try each word’s prefixes shortest first against a set of roots.
  4. 4Code the trie. Insert roots with an end mark, then walk each word and stop at the first mark.
  5. 5Test out loud. Run cart with car and ca, and a word shorter than its root.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?