# Blogs Hub

### by Sumit Chourasia | Sep 30, 2020 | Category :coding | Tags : algorithmडेटा-संरचनाआसानhash-tableleetcode

#### Shortest Completing Word - मिनी टीवी

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.
Example 3:

Input: licensePlate = "Ah71752", words = ["suggest","letter","of","husband","easy","education","drug","prevent","writer","old"]
Output: "husband"
Example 4:

Input: licensePlate = "OgEu755", words = ["enough","these","play","wide","wonder","box","arrive","money","tax","thus"]
Output: "enough"
Example 5:

Input: licensePlate = "iMSlpe4", words = ["claim","consumer","student","camera","public","never","wonder","simple","thought","use"]
Output: "simple"

``````public class Solution {
public string ShortestCompletingWord(string licensePlate, string[] words) {
var map = new Dictionary<char,int>();
}
else{
}
}
}

int minLen = int.MaxValue;
string ans = string.Empty;
for(int i=0;i<words.Length;i++){
var wordMap = new Dictionary<char,int>();
var word = words[i].ToLower();
for(int j=0;j<words[i].Length;j++){
if(wordMap.ContainsKey(word[j])){
wordMap[word[j]]++;
}
else{
}
}

bool found = true;
foreach(var item in map){
if(wordMap.ContainsKey(item.Key)){
if(wordMap[item.Key]<item.Value){
found = false;
break;
}
}
else{
found = false;
break;
}
}

if(found && words[i].Length<minLen){
minLen = words[i].Length;
ans = words[i];
}
}

return ans;
}
}``````

Time Complexity: O(m*n)

Space complexity: O(m*n)

Where m and n are the total number of strings and length of strings.

Contributed By: Sumit Chourasia
Contributed By: Sumit Chourasia
Contributed By: Sumit Chourasia
Contributed By: Sumit Chourasia