0748. Shortest Completing Word

748. Shortest Completing Word #

Problem #

Find the minimum length word from a given dictionary words, which has all the letters from the string licensePlate. Such a word is said to complete the given string licensePlate

Here, for letters we ignore case. For example, "P" on the licensePlate still matches "p" on the word.

It is guaranteed an answer exists. If there are multiple answers, return the one that occurs first in the array.

The license plate might have the same letter occurring multiple times. For example, given a licensePlate of "PP", the word "pair" does not complete the licensePlate, but the word "supper" does.

Example 1:

Input: licensePlate = "1s3 PSt", words = ["step", "steps", "stripe", "stepple"]
Output: "steps"
Explanation: The smallest length word that contains the letters "S", "P", "S", and "T".
Note that the answer is not "step", because the letter "s" must occur in the word twice.
Also note that we ignored case for the purposes of comparing whether a letter exists in the word.

Example 2:

Input: licensePlate = "1s3 456", words = ["looks", "pest", "stew", "show"]
Output: "pest"
Explanation: There are 3 smallest length words that contains the letters "s".
We return the one that occurred first.

Note:

  1. licensePlate will be a string with length in range [1, 7].
  2. licensePlate will contain digits, spaces, or letters (uppercase or lowercase).
  3. words will have a length in the range [10, 1000].
  4. Every words[i] will consist of lowercase letters, and have length in range [1, 15].

Problem Summary #

If a word in the word list (words) contains all the letters in the license plate (licensePlate), then we call it a completing word. Among all completing words, the shortest word is called the shortest completing word.

When matching letters in the license plate, words are case-insensitive; for example, the “P” in the license plate can still match the “p” letter in a word. It is guaranteed that a shortest completing word exists. When multiple words all satisfy the matching condition for the shortest completing word, choose the one that appears earliest in the word list. The license plate may contain multiple identical characters. For example: for the license plate “PP”, the word “pair” cannot match, but “supper” can.

Note:

  • The length of the license plate (licensePlate) is in the range [1, 7].
  • The license plate (licensePlate) will contain digits, spaces, or letters (uppercase and lowercase).
  • The length of the word list (words) is in the range [10, 1000].
  • Each word words[i] is lowercase and has length in the range [1, 15].

Solution Ideas #

  • Given an array, find the shortest string that can contain all characters in the licensePlate string. If there are multiple strings with the shortest length, output the one with the smaller word index. This is also an easy problem, but there are 2 points to note. First, licensePlate may contain any Unicode characters, so the letter characters must be filtered out first. Second, the problem guarantees that there must be a shortest word satisfying the requirement, and case is ignored. The specific approach is simply to simulate according to the problem statement.

Code #


package leetcode

import "unicode"

func shortestCompletingWord(licensePlate string, words []string) string {
	lp := genCnter(licensePlate)
	var ret string
	for _, w := range words {
		if match(lp, w) {
			if len(w) < len(ret) || ret == "" {
				ret = w
			}
		}
	}
	return ret
}

func genCnter(lp string) [26]int {
	cnter := [26]int{}
	for _, ch := range lp {
		if unicode.IsLetter(ch) {
			cnter[unicode.ToLower(ch)-'a']++
		}
	}
	return cnter
}

func match(lp [26]int, w string) bool {
	m := [26]int{}
	for _, ch := range w {
		m[ch-'a']++
	}
	for k, v := range lp {
		if m[k] < v {
			return false
		}
	}
	return true
}


Calendar Jun 25, 2026
Edit Edit this page
Total visits:   You are visitor No.
中文