Shortest Completing Word - Problem

You're given a license plate and a list of candidate words. Your mission is to find the shortest word that contains all the letters from the license plate!

Rules:

  • ๐Ÿ”ค Only letters matter - ignore numbers and spaces in the license plate
  • ๐Ÿ“ Case doesn't matter - treat 'A' and 'a' as the same
  • ๐Ÿ”ข If a letter appears multiple times in the license plate, the word must have at least that many occurrences
  • ๐Ÿ“ Return the shortest completing word. If there's a tie, return the first one

Example: If licensePlate = "aBc 12c", you need a word with at least: 1ร—'a', 1ร—'b', and 2ร—'c'. Words like "abccdef" or "caaacab" would work!

Input & Output

example_1.py โ€” Basic Example
$ Input: licensePlate = "1s3 PSt", words = ["step", "steps", "stripe", "stepple"]
โ€บ Output: "steps"
๐Ÿ’ก Note: License plate contains letters: s, P, s, t โ†’ s(2), p(1), t(1). 'steps' is the shortest word containing at least 2 s's, 1 p, and 1 t.
example_2.py โ€” Case Insensitive
$ Input: licensePlate = "1s3 456", words = ["looks", "pest", "stew", "show"]
โ€บ Output: "pest"
๐Ÿ’ก Note: License plate contains only 's' (case insensitive). All words contain 's', but 'pest' is shortest at 4 characters.
example_3.py โ€” Multiple Requirements
$ Input: licensePlate = "Ah71752", words = ["suggest", "letter", "of", "husband", "easy", "education", "drug", "prevent", "writer", "old"]
โ€บ Output: "husband"
๐Ÿ’ก Note: License plate contains 'A' and 'h' (case insensitive: a(1), h(1)). 'husband' contains both required letters and is the first shortest match.

Visualization

Tap to expand
License Plate Word Matching ProcessLicense Plate"aBc 12c"Extract Lettersa, b, c, cCount Frequencya:1, b:1, c:2"step""steps""stripe""stepple"โŒ No 'c'โœ… Has allโœ… Has allโœ… Has allFind Shortest Valid Word"steps" (5 chars) wins over"stripe" (6) and "stepple" (7)๐ŸŽฏ Result: "steps"โœ… Contains: s(2), t(1), e(1), p(1)โœ… Satisfies: a(1), b(1), c(2) โŒ Wait...Time: O(k + mร—n) | Space: O(1) | k=plate length, m=words, n=avg word length
Understanding the Visualization
1
Extract Recipe Requirements
From 'aBc 12c', extract letters: a(1), b(1), c(2) - ignore numbers and spaces
2
Build Shopping List
Create frequency map showing exactly what letters and quantities we need
3
Check Each Recipe
For each word, count its letters and verify it has enough of each required letter
4
Find Shortest Match
Among valid words, return the shortest one (first in case of ties)
Key Takeaway
๐ŸŽฏ Key Insight: By preprocessing the license plate requirements once and reusing them for all word validations, we avoid redundant work and achieve optimal O(k + mร—n) time complexity.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(m ร— n ร— k)

Where m is number of words, n is average word length, and k is license plate length. We process the license plate k times for each of m words.

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using a few variables to track counts and the current best word

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค licensePlate.length โ‰ค 7
  • 1 โ‰ค words.length โ‰ค 1000
  • 1 โ‰ค words[i].length โ‰ค 15
  • licensePlate and words[i] consist of uppercase and lowercase English letters and digits
  • It is guaranteed that an answer exists
Asked in
Amazon 25 Microsoft 18 Google 12 Apple 8
28.5K Views
Medium Frequency
~15 min Avg. Time
892 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