Problem 524 · LeetCode 524(opens in a new tab)
Longest Word in Dictionary through Deleting
MediumSorting
You are given a string s and a list of words, dictionary. A word can be formed from s if you can delete some letters of s (possibly none) and read the word from what is left, without reordering anything.
Return the longest word that can be formed. If several of the longest words can be formed, return the one that comes first in dictionary order. If no word can be formed, return the empty string.
Example 1
Input: s = "trailmix", dictionary = ["tail", "mix", "trim", "rail"] Output: "rail" Why: tail, trim and rail all fit and have 4 letters; rail sorts first
2 more examples and the constraints
Example 2
Input: s = "abc", dictionary = ["d", "cb"] Output: "" Why: s has no d, and its c comes after its b
Example 3
Input: s = "zebra", dictionary = ["b", "a", "z"] Output: "a" Why: all three fit with one letter; a sorts first
Constraints
- 1 ≤ length of s ≤ 1,000
- 1 ≤ number of words ≤ 1,000, each 1 to 1,000 letters long
- s and every word use lowercase English letters only
Your turn first
Have a go on paper or in your own editor, then reveal the approaches from the simplest up.
- Sort, then checkO(d·m log d + d·n)
- One pass, keep the bestO(d · (n + m))
The idea in one sentence
The pattern, how to spot it, and the best complexity.
Side by side
| Approach | Time | Relative work for 1,000 words of 1,000 letters | Verdict |
|---|---|---|---|
| Sort, then check | O(d·m log d + d·n) | Passes; costs a sort | |
| One pass, keep the best | O(d · (n + m)) | Best; no sort, no copy |
d = number of words, m = longest word, n = length of s.
Edge cases to test
- Nothing fits. s = "abc" with ["d", "cb"] returns the empty string.
- Ties on length. Several longest words fit: return the one that sorts first, as with "rail" over "tail".
- Word longer than s. It can never fit; the pointer into the word runs out of letters of s.
- Repeated letters. "aab" needs two separate a's in s before a b, so it fits "abab" but not "ab".
- Word equal to s. Deleting nothing is allowed, so s itself fits.
Common mistakes
- Checking that the letters exist in s without checking their order, so "cb" wrongly fits "abc".
- On a tie in length, keeping the first word seen instead of the one earlier in dictionary order.
- Sorting only by length and forgetting the tie-break, so the sort-then-check version returns the wrong tied word.
How to talk through it
- 1Clarify. Can letters be reordered? What do I return when nothing fits, and how are ties broken?
- 2Name the check. A word can be formed exactly when it is a subsequence of s; two pointers test that in one walk of s.
- 3Give the simple plan. Sort longest first, ties in dictionary order, and return the first word that fits.
- 4Drop the sort. Check each word once and keep the best, skipping words that cannot beat it.
- 5Test out loud. Run the no-fit case, a tie on length and a word that has the right letters in the wrong order.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?