-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.cpp
34 lines (32 loc) · 891 Bytes
/
Solution.cpp
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) {
vector<int> counter = count(licensePlate);
int n = 16;
string ans;
for (auto& word : words)
{
if (n <= word.size()) continue;
vector<int> t = count(word);
if (check(counter, t))
{
n = word.size();
ans = word;
}
}
return ans;
}
vector<int> count(string& word) {
vector<int> counter(26);
for (char& c : word)
if (isalpha(c))
++counter[tolower(c) - 'a'];
return counter;
}
bool check(vector<int>& counter1, vector<int>& counter2) {
for (int i = 0; i < 26; ++i)
if (counter1[i] > counter2[i])
return false;
return true;
}
};