0%

748. 最短补全词

748. 最短补全词

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution {
public:
string shortestCompletingWord(string licensePlate, vector<string>& words) {
int map[26]{ 0 };
int mL = INT_MAX, ret = -1;
int count = 0;
for(auto ch : licensePlate)
if(ch != ' ' && !(ch >= '0' && ch <= '9'))
if(++map[lower(ch)] == 1)
++count;
for(int i = 0, n = words.size(); i < n; ++i)
{
int m[26]{0};
copy(map, map + 25, m);
auto tmp = count;
for(auto& ch : words[i])
if(--m[lower(ch)] == 0)
--tmp;
if(tmp == 0 && words[i].size() < mL)
{
mL = words[i].size();
ret = i;
}
}
return words[ret];
}
private:
int lower(char ch)
{
if(ch >= 'A' && ch <= 'Z')
return ch - 'A';
return ch - 'a';
}
};