Shortest Completing Word - Problem

Given a string licensePlate and an array of strings words, find the shortest completing word in words.

A completing word is a word that contains all the letters in licensePlate. Ignore numbers and spaces in licensePlate, and treat letters as case insensitive.

If a letter appears more than once in licensePlate, then it must appear in the word the same number of times or more.

For example, if licensePlate = "aBc 12c", then it contains letters 'a', 'b' (ignoring case), and 'c' twice. Possible completing words are "abccdef", "caaacab", and "cbca".

Return the shortest completing word in words. It is guaranteed an answer exists. If there are multiple shortest completing words, return the first one that occurs in words.

Input & Output

Example 1 — Basic Case
$ Input: licensePlate = "1s3 PSt", words = ["step", "steps", "stripe", "stepple"]
Output: "step"
💡 Note: The license plate contains letters 's', 'p', 't' (ignoring case and numbers). "step" has s:1, t:1, e:1, p:1 which covers all requirements and has length 4. "steps" also works but has length 5. "stripe" and "stepple" are longer, so "step" is the shortest completing word.
Example 2 — Multiple Same Letters
$ Input: licensePlate = "1s3 333", words = ["S", "sss", "look"]
Output: "S"
💡 Note: The license plate contains only 's' (once). "S" has s:1 which satisfies the requirement and has length 1. "sss" also works but has length 3. "look" doesn't contain 's'. So "S" is the shortest completing word.
Example 3 — Case Sensitivity
$ Input: licensePlate = "Ah71752", words = ["suggest", "letter", "of", "husband", "easy", "education", "drug", "prevent", "writer", "old"]
Output: "husband"
💡 Note: The license plate contains 'A' and 'h' (case insensitive, so 'a' and 'h'). "husband" contains both 'a' and 'h', and among valid words, it appears first in the array with reasonable length.

Constraints

  • 1 ≤ licensePlate.length ≤ 7
  • 1 ≤ words.length ≤ 1000
  • 1 ≤ words[i].length ≤ 15
  • licensePlate consists of digits, letters, and spaces
  • words[i] consists of lowercase English letters

Visualization

Tap to expand
Shortest Completing Word INPUT licensePlate: "1s3 PSt" Letters needed (lowercase): s p s t Need: s(2), p(1), t(1) words array: "step" "steps" "stripe" "stepple" ALGORITHM STEPS 1 Count License Letters Build frequency map: {s:2,p:1,t:1} 2 Check Each Word Count letters in each word 3 Validate Completing Word has >= required letters 4 Track Shortest Keep shortest completing word Word Analysis Word Has s,s,p,t? Status step s:1 (need 2) FAIL steps s:2,p:1,t:1 OK (5) stripe s:2,p:1,t:1 OK (6) stepple s:1 (need 2) FAIL FINAL RESULT Completing words found: "steps" Length: 5 (SHORTEST) "stripe" Length: 6 Winner Selection: "steps" (len=5) vs "stripe" (len=6) 5 < 6, so "steps" wins (First shortest is returned) Output: "steps" Key Insight: Use a frequency count (hash map) for the license plate letters. For each word, count its letters and verify that every required letter appears at least as many times as needed. Track the shortest valid word found. Time: O(n * m) where n = words count, m = avg word length. Space: O(1) for 26-letter frequency arrays. TutorialsPoint - Shortest Completing Word | Optimal Solution
Asked in
Amazon 25 Microsoft 18 Google 15
25.0K 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