↓Skip to main content
  1. LeetCode/

LeetCode 748: Shortest Completing Word

·2 mins· ·
LeetCode Easy Array Hash-Table String
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-09-03

  • Submissions: Wrong Answer → Runtime Error ×2 → Wrong Answer ×6 → Accepted

Approach
#

Keep only the plate’s letters, lowercase, and count (Hash Table Template 1). A word completes it when it covers every needed count (as in LC 383); among those take the shortest, and since ties go to the earliest, update only when strictly shorter.

My Attempts
#

This took 10 submissions, one bug after another:

  • Attempt 1’s inner loop used the outer loop’s leftover i, so it never checked the word’s characters (and raised KeyError when the plate ended in a non-letter); the next two were KeyError REs.
  • Then d[i] = d.get(i.lower(), 0) + 1 stored the original case but read lowercase: "B" was stored as B, so the word "b" never matched and it returned None.
  • With lowercase keys, for word in set(words) scrambled the order and returned the first match: "a", ["a", "bda"] returned "bda". Without set it still returned the first match rather than the shortest: "B", ["ccb", "b"] returned "ccb".
  • Searching for the shortest started from "", so len(word) < 0 was never true and it returned an empty string. Adding a res == "" check passed.

Solution
#

class Solution:
    def shortestCompletingWord(self, licensePlate: str, words: List[str]) -> str:
        need = Counter(c.lower() for c in licensePlate if c.isalpha())
        best = None
        for w in words:
            if not (need - Counter(w)) and (best is None or len(w) < len(best)):
                best = w
        return best

Complexity
#

Time O(total characters), space O(k).

Mistakes to Avoid
#

  • Lowercase the counting keys.
  • Do not turn words into a set: it loses order, which decides ties.
  • The first match is not necessarily the shortest; scan everything.
  • With "" as the initial answer, len(w) < len("") is never true.

Interview Explanation
#

I count the plate’s letters in lowercase, check each word’s coverage, and keep a match only when strictly shorter, so ties keep the earliest word. Time is linear in the total length.

Related

LeetCode 599: Minimum Index Sum of Two Lists
·2 mins
LeetCode Easy Array Hash-Table String
LeetCode 599 note: map list1’s strings to indices, sum both indices while scanning list2, and keep every string with the minimum sum
LeetCode 500: Keyboard Row
·1 min
LeetCode Easy Array Hash-Table String
LeetCode 500 note: make a set for each keyboard row and keep words whose lowercase character set is a subset of one row
LeetCode 705: Design HashSet
·2 mins
LeetCode Easy Array Hash-Table Linked-List Design
LeetCode 705 note: a fixed number of buckets, with the key modulo the bucket count choosing the bucket and a list per bucket handling collisions
LeetCode 697: Degree of an Array
·2 mins
LeetCode Easy Array Hash-Table
LeetCode 697 note: record each value’s count, first index and last index; among the most frequent values, take the shortest span
LeetCode 645: Set Mismatch
·2 mins
LeetCode Easy Array Hash-Table Bit-Manipulation Sorting
LeetCode 645 note: use sums: missing = sum of 1..n - sum of the set, duplicate = array sum - sum of the set
LeetCode 575: Distribute Candies
·1 min
LeetCode Easy Array Hash-Table
LeetCode 575 note: at most n / 2 candies can be eaten and at most len(set) kinds exist; the answer is the smaller of the two