I like to read this solution as a small machine: keep the useful information, throw away the noise. For 748. Shortest Completing Word, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: straightforward implementation.
Guide
When?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are shortestCompletingWord.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01/**
02Find the minimum length word from a given dictionary words, which has all the letters from the string licensePlate. Such a word is said to complete the given string licensePlate
03
04Here, for letters we ignore case. For example, "P" on the licensePlate still matches "p" on the word.
05
06It is guaranteed an answer exists. If there are multiple answers, return the one that occurs first in the array.
07
08The license plate might have the same letter occurring multiple times. For example, given a licensePlate of "PP", the word "pair" does not complete the licensePlate, but the word "supper" does.
09
10Example 1:
11Input: licensePlate = "1s3 PSt", words = ["step", "steps", "stripe", "stepple"]
12Output: "steps"
13Explanation: The smallest length word that contains the letters "S", "P", "S", and "T".
14Note that the answer is not "step", because the letter "s" must occur in the word twice.
15Also note that we ignored case for the purposes of comparing whether a letter exists in the word.
16Example 2:
17Input: licensePlate = "1s3 456", words = ["looks", "pest", "stew", "show"]
18Output: "pest"
19Explanation: There are 3 smallest length words that contains the letters "s".
20We return the one that occurred first.
21Note:
22licensePlate will be a string with length in range [1, 7].
23licensePlate will contain digits, spaces, or letters (uppercase or lowercase).
24words will have a length in the range [10, 1000].
25Every words[i] will consist of lowercase letters, and have length in range [1, 15].
26**/
27
28//Runtime: 40 ms, faster than 40.41% of C++ online submissions for Shortest Completing Word.
29//Memory Usage: 17.1 MB, less than 21.88% of C++ online submissions for Shortest Completing Word.
30
31class Solution {
32public:
33 string shortestCompletingWord(string licensePlate, vector<string>& words) {
34 map<char, int> charCount;
35
36 for(char c : licensePlate){
37 if(!isalpha(c)) continue;
38 c = tolower(c);
39 if(charCount.find(c)==charCount.end()){
40 charCount[c]=1;
41 }else{
42 charCount[c]+=1;
43 }
44 }
45
46 // for(map<char, int>::iterator it = charCount.begin(); it!=charCount.end(); it++){
47 // cout << it->first << " " << it->second << endl;
48 // }
49
50 string ans = "";
51 for(string word : words){
52 map<char, int> tmp = charCount;
53 for(char c : word){
54 if(tmp.find(c)!=tmp.end()){
55 tmp[c]--;
56 if(tmp[c]==0) tmp.erase(c);
57 }
58 }
59 // cout << word << " " << tmp.size() << endl;
60 if(tmp.size()==0){
61 if(ans==""){
62 ans = word;
63 }else if(word.size() < ans.size()){
64 ans = word;
65 }
66 }
67 }
68
69 return ans;
70 }
71};
Cost