-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0748.cpp
More file actions
37 lines (36 loc) · 855 Bytes
/
0748.cpp
File metadata and controls
37 lines (36 loc) · 855 Bytes
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
35
36
37
class Solution {
public:
string shortestCompletingWord(string licensePlate, vector<string> &words) {
return func1(licensePlate, words);
}
string func1(string licensePlate, vector<string> &words) {
int lpSet[26] = {0};
for (char c : licensePlate) {
if (isalpha(c)) {
lpSet[tolower(c) - 'a']++;
}
}
int shortest = -1;
for (int i = 0; i < words.size(); i++) {
int wdSet[26] = {0};
for (char c : words[i]) {
wdSet[c - 'a']++;
}
int j = 0;
for (j; j < 26; j++) {
if (wdSet[j] < lpSet[j])
break;
}
if (j == 26) {
if (shortest == -1) {
shortest = i;
} else {
if (words[i].length() < words[shortest].length()) {
shortest = i;
}
}
}
}
return words[shortest];
}
};