-
Notifications
You must be signed in to change notification settings - Fork 3
/
748.cpp
34 lines (34 loc) · 963 Bytes
/
748.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:
bool isValid(vector<int> hash, int count, string& word) {
for (auto c : word) {
c -= 'a';
if (hash[c] > 0) {
hash[c]--;
count--;
}
}
return count == 0;
}
string shortestCompletingWord(string licensePlate, vector<string>& words) {
vector<int> hash(26, 0);
int count = 0;
for (auto& c : licensePlate) {
if (c == ' ') continue;
if (c >= '0' && c <= '9') continue;
if (c >= 'A' && c <= 'Z') c -= 'A';
else if (c >= 'a' && c <= 'z') c -= 'a';
hash[c]++;
count++;
}
string res;
for (auto& word : words) {
if (isValid(hash, count, word)) {
if (res.size() == 0 || word.size() < res.size()) {
res = word;
}
}
}
return res;
}
};