{x}
blog image

Shortest Completing Word

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.

 

Example 1:

Input: licensePlate = "1s3 PSt", words = ["step","steps","stripe","stepple"]
Output: "steps"
Explanation: licensePlate contains letters 's', 'p', 's' (ignoring case), and 't'.
"step" contains 't' and 'p', but only contains 1 's'.
"steps" contains 't', 'p', and both 's' characters.
"stripe" is missing an 's'.
"stepple" is missing an 's'.
Since "steps" is the only word containing all the letters, that is the answer.

Example 2:

Input: licensePlate = "1s3 456", words = ["looks","pest","stew","show"]
Output: "pest"
Explanation: licensePlate only contains the letter 's'. All the words contain 's', but among these "pest", "stew", and "show" are shortest. The answer is "pest" because it is the word that appears earliest of the 3.

 

Constraints:

  • 1 <= licensePlate.length <= 7
  • licensePlate contains digits, letters (uppercase or lowercase), or space ' '.
  • 1 <= words.length <= 1000
  • 1 <= words[i].length <= 15
  • words[i] consists of lower case English letters.

Solution Explanation: Finding the Shortest Completing Word

This problem involves finding the shortest word from a given list that contains all the letters present in a license plate, respecting letter case and frequency. The solution employs a counting approach for efficient comparison.

Approach

  1. License Plate Processing:

    • We first process the licensePlate string to count the frequency of each letter (ignoring case and non-alphabetic characters). A hash map (or array for simplicity) is ideal for this; we'll use an array indexed by the letter's position in the alphabet (a=0, b=1,...).
  2. Word Iteration:

    • The code iterates through each word in the words list.
  3. Word Frequency Counting:

    • For each word, we count the letter frequencies similarly to step 1.
  4. Comparison and Shortest Word Selection:

    • We compare the word's letter frequencies with the licensePlate's frequencies. A word is a "completing word" only if its frequency for each letter is greater than or equal to the frequency in the licensePlate.
    • If a word is a completing word, we check its length against the current shortest completing word found so far. We update the shortest completing word if a shorter one is found.
  5. Return Value:

    • The function returns the shortest completing word.

Time and Space Complexity

  • Time Complexity: O(mn), where 'm' is the length of the words array, and 'n' is the maximum length of a word in words. This is because we iterate through each word and count its letter frequencies which is a linear process based on the word's length. The initial processing of the licensePlate is O(k), where k is the length of licensePlate, which is dominated by O(mn).
  • Space Complexity: O(1). We use arrays of size 26 (to store letter counts) which takes constant space irrespective of input size.

Code (Python)

from collections import Counter
 
def shortestCompletingWord(licensePlate, words):
    plate_counts = Counter(c.lower() for c in licensePlate if c.isalpha())  # Count license plate letter frequencies
    shortest_word = ""
    for word in words:
        word_counts = Counter(word)
        if all(word_counts[char] >= plate_counts[char] for char in plate_counts): # Check if word completes
            if not shortest_word or len(word) < len(shortest_word):
                shortest_word = word
    return shortest_word

The code in other languages (Java, C++, Go, TypeScript, Rust) follows a similar structure, adapting the syntax and data structures accordingly. The core algorithm remains consistent across all implementations.