forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
32 lines (32 loc) · 809 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
class Solution {
public:
string shortestCompletingWord(string licensePlate, vector<string>& words) {
int cnt[26]{};
for (char& c : licensePlate) {
if (isalpha(c)) {
++cnt[tolower(c) - 'a'];
}
}
string ans;
for (auto& w : words) {
if (ans.size() && ans.size() <= w.size()) {
continue;
}
int t[26]{};
for (char& c : w) {
++t[c - 'a'];
}
bool ok = true;
for (int i = 0; i < 26; ++i) {
if (cnt[i] > t[i]) {
ok = false;
break;
}
}
if (ok) {
ans = w;
}
}
return ans;
}
};