Find the minimum length word from a givendictionary words, which has all the letters from the string licensePlate. Sucha word is said to complete the given string licensePlate
Here, for letters we ignore case. For example, "P" onthe licensePlate still matches "p" onthe word.
It is guaranteed an answer exists. If there aremultiple answers, return the one that occurs first in the array.
The license plate might have the same letter occurringmultiple times. For example, given a licensePlate of "PP", the word "pair" doesnot complete the licensePlate, but the word "supper" does.
Example 1:
Input: licensePlate = "1s3 PSt",words = ["step", "steps", "stripe","stepple"]
Output: "steps"
Explanation: The smallest length word that containsthe letters "S", "P", "S", and "T".
Note that the answer is not "step", because theletter "s" must occur in the word twice.
Also note that we ignored case for the purposes of comparingwhether a letter exists in the word.
Example 2:
Input: licensePlate = "1s3 456",words = ["looks", "pest", "stew","show"]
Output: "pest"
Explanation: There are 3 smallest length words thatcontains the letters "s".
We return the one that occurred first.
Note:
1. licensePlate will be a string withlength in range [1, 7].
2. licensePlate will contain digits,spaces, or letters (uppercase or lowercase).
3. words will have a length inthe range [10, 1000].
4. Every words[i] will consist oflowercase letters, and have length in range [1, 15].
这题就是给一个string的数组,然后再给一个licensePlate问你string数组里面拥有licensePlate全部字母的长度最小的一个字符串是多少
这题其实很简单的,通过这题的编译通过,我学会了以下技巧:在ascll表里面,A为65,a为97,A比a大32,把小写转换成大写 char a=a-32,判断大小写if(‘a’<=a&&a<=’z’),小写转换成int,int c=a-‘a’;
我还学会了静态模板的用法 比如#define REP(s,t) for(int i=s;i<t;i++)
这里的s,t代表的是字符串,用来替换后面表达式里面的相应的字符串
还有就是在循环体里面的if语句的下一句,在执行的时候断点会不停的到达,但是不一定会执行,也就是说在程序里面,断点到达的语句不一定会执行
class Solution {
#include <string>
public:
bool check(int num[], string b)
{
intnumb[27] = { 0 };
for(int i = 0; i < b.length(); i++)
{
if(b[i] >= 'A'&&b[i] <= 'Z')
numb[b[i]- 'A' + 1] += 1;
if(b[i] >= 'a'&&b[i] <= 'z')
numb[b[i]- 'a' + 1] += 1;
}
for(int i = 1; i <= 26; i++)
if(num[i] != 0)
if(num[i]>numb[i])
return false;
returntrue;
}
string shortestCompletingWord(stringlicensePlate, vector<string>& words) {
intnum[27] = { 0 };
for(int i = 0; i < licensePlate.length(); i++)
{
if(licensePlate[i] >= 'A'&&licensePlate[i] <= 'Z')
num[licensePlate[i]- 'A' + 1] += 1;
if(licensePlate[i] >= 'a'&&licensePlate[i] <= 'z')
num[licensePlate[i]- 'a' + 1] += 1;
}
intminLength, minLengthIndex;
minLength= 100;
for(int i = 0; i < words.size(); i++)
{
if(check(num,words[i]))
{
if(words[i].size() < minLength)
{
minLengthIndex= i; minLength = words[i].size();
}
}
}
returnwords[minLengthIndex];
}
};