Skip to content

Commit 15c2c72

Browse files
committed
Add solution #1170
1 parent 796f47b commit 15c2c72

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,163 LeetCode solutions in JavaScript
1+
# 1,164 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -911,6 +911,7 @@
911911
1162|[As Far from Land as Possible](./solutions/1162-as-far-from-land-as-possible.js)|Medium|
912912
1163|[Last Substring in Lexicographical Order](./solutions/1163-last-substring-in-lexicographical-order.js)|Hard|
913913
1169|[Invalid Transactions](./solutions/1169-invalid-transactions.js)|Medium|
914+
1170|[Compare Strings by Frequency of the Smallest Character](./solutions/1170-compare-strings-by-frequency-of-the-smallest-character.js)|Medium|
914915
1189|[Maximum Number of Balloons](./solutions/1189-maximum-number-of-balloons.js)|Easy|
915916
1200|[Minimum Absolute Difference](./solutions/1200-minimum-absolute-difference.js)|Easy|
916917
1206|[Design Skiplist](./solutions/1206-design-skiplist.js)|Hard|
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* 1170. Compare Strings by Frequency of the Smallest Character
3+
* https://leetcode.com/problems/compare-strings-by-frequency-of-the-smallest-character/
4+
* Difficulty: Medium
5+
*
6+
* Let the function f(s) be the frequency of the lexicographically smallest character in a non-empty
7+
* string s. For example, if s = "dcce" then f(s) = 2 because the lexicographically smallest
8+
* character is 'c', which has a frequency of 2.
9+
*
10+
* You are given an array of strings words and another array of query strings queries. For each
11+
* query queries[i], count the number of words in words such that f(queries[i]) < f(W) for each
12+
* W in words.
13+
*
14+
* Return an integer array answer, where each answer[i] is the answer to the ith query.
15+
*/
16+
17+
/**
18+
* @param {string[]} queries
19+
* @param {string[]} words
20+
* @return {number[]}
21+
*/
22+
var numSmallerByFrequency = function(queries, words) {
23+
const wordFrequencies = words.map(getMinCharFreq).sort((a, b) => a - b);
24+
25+
return queries.map(query => {
26+
const queryFreq = getMinCharFreq(query);
27+
let left = 0;
28+
let right = wordFrequencies.length;
29+
30+
while (left < right) {
31+
const mid = Math.floor((left + right) / 2);
32+
if (wordFrequencies[mid] <= queryFreq) {
33+
left = mid + 1;
34+
} else {
35+
right = mid;
36+
}
37+
}
38+
39+
return wordFrequencies.length - left;
40+
});
41+
42+
function getMinCharFreq(str) {
43+
let minChar = 'z';
44+
let freq = 0;
45+
for (const char of str) {
46+
if (char < minChar) {
47+
minChar = char;
48+
freq = 1;
49+
} else if (char === minChar) {
50+
freq++;
51+
}
52+
}
53+
return freq;
54+
}
55+
};

0 commit comments

Comments
 (0)