Welcome to Subscribe On Youtube

748. Shortest Completing Word

Description

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.

Solutions

Solution 1: Counting

First, we use a hash table or an array $cnt$ of length $26$ to count the frequency of each letter in the string licensePlate. Note that we convert all letters to lowercase for counting.

Then, we traverse each word $w$ in the array words. If the length of the word $w$ is longer than the length of the answer $ans$, we directly skip this word. Otherwise, we use another hash table or an array $t$ of length $26$ to count the frequency of each letter in the word $w$. If for any letter, the frequency of this letter in $t$ is less than the frequency of this letter in $cnt$, we can also directly skip this word. Otherwise, we have found a word that meets the conditions, and we update the answer $ans$ to the current word $w$.

The time complexity is $O(n \times |\Sigma|)$, and the space complexity is $O(|\Sigma|)$. Here, $n$ is the length of the array words, and $\Sigma$ is the character set. In this case, the character set is all lowercase letters, so $|\Sigma| = 26$.

  • 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;
        }
    }
    
  • class Solution {
    public:
        string shortestCompletingWord(string licensePlate, vector<string>& words) {
            int cnt[26]{};
            for (char& c : licensePlate) {
                if (isalpha(c)) {
                    ++cnt[tolower(c) - 'a'];
                }
            }
            string ans;
            for (auto& w : words) {
                if (ans.size() && ans.size() <= w.size()) {
                    continue;
                }
                int t[26]{};
                for (char& c : w) {
                    ++t[c - 'a'];
                }
                bool ok = true;
                for (int i = 0; i < 26; ++i) {
                    if (cnt[i] > t[i]) {
                        ok = false;
                        break;
                    }
                }
                if (ok) {
                    ans = w;
                }
            }
            return ans;
        }
    };
    
  • 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
    
    
  • 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
    }
    
  • 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;
    }
    
    
  • 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
        }
    }
    
    

All Problems

All Solutions