classSolution{public:stringshortestCompletingWord(stringlicensePlate,vector<string>&words){stringans(16,'.');vector<int>count(26);for(constcharc:licensePlate)if(isalpha(c))++count[tolower(c)-'a'];for(conststring&word:words)if(word.length()<ans.length()&&isComplete(count,getCount(word)))ans=word;returnans;}private:// Check if c1 is a subset of c2boolisComplete(constvector<int>&c1,constvector<int>c2){for(inti=0;i<26;++i)if(c1[i]>c2[i])returnfalse;returntrue;}vector<int>getCount(conststring&word){vector<int>count(26);for(constcharc:word)++count[c-'a'];returncount;}};
classSolution{publicStringshortestCompletingWord(StringlicensePlate,String[]words){Stringans="****************";int[]count=newint[26];for(charc:licensePlate.toCharArray())if(Character.isLetter(c))++count[Character.toLowerCase(c)-'a'];for(finalStringword:words)if(word.length()<ans.length()&&isComplete(count,getCount(word)))ans=word;returnans;}// Check if c1 is a subset of c2privatebooleanisComplete(int[]c1,int[]c2){for(inti=0;i<26;++i)if(c1[i]>c2[i])returnfalse;returntrue;}privateint[]getCount(finalStringword){int[]count=newint[26];for(finalcharc:word.toCharArray())++count[c-'a'];returncount;}}