Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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 <= 7licensePlate contains digits, letters (uppercase or lowercase), or space ' '.1 <= words.length <= 10001 <= words[i].length <= 15words[i] consists of lower case English letters.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
"1s3 PSt" ["step","steps","stripe","stepple"]
"1s3 456" ["looks","pest","stew","show"]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #748: Shortest Completing Word
class Solution {
public String shortestCompletingWord(String licensePlate, String[] words) {
int[] cnt = new int[26];
for (int i = 0; i < licensePlate.length(); ++i) {
char c = licensePlate.charAt(i);
if (Character.isLetter(c)) {
cnt[Character.toLowerCase(c) - 'a']++;
}
}
String ans = "";
for (String w : words) {
if (!ans.isEmpty() && w.length() >= ans.length()) {
continue;
}
int[] t = new int[26];
for (int i = 0; i < w.length(); ++i) {
t[w.charAt(i) - 'a']++;
}
boolean ok = true;
for (int i = 0; i < 26; ++i) {
if (t[i] < cnt[i]) {
ok = false;
break;
}
}
if (ok) {
ans = w;
}
}
return ans;
}
}
// Accepted solution for LeetCode #748: Shortest Completing Word
func shortestCompletingWord(licensePlate string, words []string) (ans string) {
cnt := [26]int{}
for _, c := range licensePlate {
if unicode.IsLetter(c) {
cnt[unicode.ToLower(c)-'a']++
}
}
for _, w := range words {
if len(ans) > 0 && len(ans) <= len(w) {
continue
}
t := [26]int{}
for _, c := range w {
t[c-'a']++
}
ok := true
for i, v := range cnt {
if t[i] < v {
ok = false
break
}
}
if ok {
ans = w
}
}
return
}
# Accepted solution for LeetCode #748: Shortest Completing Word
class Solution:
def shortestCompletingWord(self, licensePlate: str, words: List[str]) -> str:
cnt = Counter(c.lower() for c in licensePlate if c.isalpha())
ans = None
for w in words:
if ans and len(w) >= len(ans):
continue
t = Counter(w)
if all(v <= t[c] for c, v in cnt.items()):
ans = w
return ans
// Accepted solution for LeetCode #748: Shortest Completing Word
impl Solution {
pub fn shortest_completing_word(license_plate: String, words: Vec<String>) -> String {
let mut cnt = vec![0; 26];
for c in license_plate.chars() {
if c.is_ascii_alphabetic() {
cnt[((c.to_ascii_lowercase() as u8) - b'a') as usize] += 1;
}
}
let mut ans = String::new();
for w in words {
if !ans.is_empty() && w.len() >= ans.len() {
continue;
}
let mut t = vec![0; 26];
for c in w.chars() {
t[((c as u8) - b'a') as usize] += 1;
}
let mut ok = true;
for i in 0..26 {
if t[i] < cnt[i] {
ok = false;
break;
}
}
if ok {
ans = w;
}
}
ans
}
}
// Accepted solution for LeetCode #748: Shortest Completing Word
function shortestCompletingWord(licensePlate: string, words: string[]): string {
const cnt: number[] = Array(26).fill(0);
for (const c of licensePlate) {
const i = c.toLowerCase().charCodeAt(0) - 97;
if (0 <= i && i < 26) {
++cnt[i];
}
}
let ans = '';
for (const w of words) {
if (ans.length && ans.length <= w.length) {
continue;
}
const t = Array(26).fill(0);
for (const c of w) {
++t[c.charCodeAt(0) - 97];
}
let ok = true;
for (let i = 0; i < 26; ++i) {
if (t[i] < cnt[i]) {
ok = false;
break;
}
}
if (ok) {
ans = w;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.