-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathLC0748.cpp
More file actions
executable file
·37 lines (33 loc) · 841 Bytes
/
LC0748.cpp
File metadata and controls
executable file
·37 lines (33 loc) · 841 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
/*
Problem Statement: https://leetcode.com/problems/shortest-completing-word/
Time: O(n • max_len)
Space: O(max_len)
Author: Mohammed Shoaib, github.com/Mohammed-Shoaib
*/
class Solution {
public:
string shortestCompletingWord(string licensePlate, vector<string>& words) {
string complete;
int len = numeric_limits<int>::max();
vector<int> freq = get_frequency(licensePlate);
for (string& word: words) {
if (word.length() >= len)
continue;
vector<int> cnt = get_frequency(word);
if (all_of(cnt.begin(), cnt.end(), [&, i = 0](int& x) mutable {
return x >= freq[i++];
})) {
complete = word;
len = word.length();
}
}
return complete;
}
vector<int> get_frequency(string& s) {
vector<int> freq(26);
for (char& c: s)
if (isalpha(c))
freq[tolower(c) - 'a']++;
return freq;
}
};