Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Shortest Completing Word

XPChallenge Points: 10
levelLevel: Easy

You are given a string licensePlate and an array of strings words.
Your task is to locate the shortest word from the list that fully satisfies the character requirements present in licensePlate.

Only alphabetic characters from licensePlate matter — digits, symbols, and spaces should be ignored.
Letter matching is case-insensitive.
If a letter appears multiple times in licensePlate, the chosen word must include that letter at least the same number of times.

Among all valid words, return the shortest one.
If several words share the same length, return the one that appears earliest in the list.

Example 1:

Input: licensePlate = "Rt 22 r" words = ["tiger", "truer", "rtr", "rabbit"]

Output: "rtr"

Explanation:

licensePlate contains: r(2), t(1). Valid words: "truer" (has r3, t1), "rtr" (has r2, t1). Shortest valid = "rtr".

Example 2:

Input: licensePlate = "A7cB" words = ["abacus", "back", "cab", "grab"]

Output: "cab"

Explanation:

Required letters → a, b, c (case-insensitive) Valid: "abacus", "back", "cab" Shortest is "cab".

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result