Skip to content

Commit f9968cc

Browse files
committed
1170. Compare Strings by Frequency of the Smallest Character
1 parent 8908359 commit f9968cc

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Runtime: 28 ms
2+
// Memory Usage: 14.5 MB
3+
class Solution {
4+
public:
5+
6+
int freq(string &w) {
7+
vector<int> mp(26, 0);
8+
for (char a : w) {
9+
mp[a - 'a']++;
10+
}
11+
for (int i = 0; i < 26; i++) {
12+
if (mp[i] > 0) {
13+
return mp[i];
14+
}
15+
}
16+
return -1;
17+
}
18+
19+
vector<int> numSmallerByFrequency(vector<string>& queries, vector<string>& words) {
20+
vector<int> freqs;
21+
for (string w : words) {
22+
freqs.push_back(freq(w));
23+
}
24+
sort(freqs.begin(), freqs.end());
25+
vector<int> res;
26+
for (string s : queries) {
27+
res.push_back(freqs.end() - upper_bound(freqs.begin(), freqs.end(), freq(s)));
28+
}
29+
return res;
30+
}
31+
};

0 commit comments

Comments
 (0)