Skip to content

Commit 3f4c568

Browse files
committed
Add solution #1647
1 parent 95fb2e2 commit 3f4c568

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-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,446 LeetCode solutions in JavaScript
1+
# 1,447 LeetCode solutions in JavaScript
22

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

@@ -1269,6 +1269,7 @@
12691269
1642|[Furthest Building You Can Reach](./solutions/1642-furthest-building-you-can-reach.js)|Medium|
12701270
1643|[Kth Smallest Instructions](./solutions/1643-kth-smallest-instructions.js)|Hard|
12711271
1646|[Get Maximum in Generated Array](./solutions/1646-get-maximum-in-generated-array.js)|Easy|
1272+
1647|[Minimum Deletions to Make Character Frequencies Unique](./solutions/1647-minimum-deletions-to-make-character-frequencies-unique.js)|Medium|
12721273
1657|[Determine if Two Strings Are Close](./solutions/1657-determine-if-two-strings-are-close.js)|Medium|
12731274
1668|[Maximum Repeating Substring](./solutions/1668-maximum-repeating-substring.js)|Easy|
12741275
1669|[Merge In Between Linked Lists](./solutions/1669-merge-in-between-linked-lists.js)|Medium|
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* 1647. Minimum Deletions to Make Character Frequencies Unique
3+
* https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/
4+
* Difficulty: Medium
5+
*
6+
* A string s is called good if there are no two different characters in s that have the
7+
* same frequency.
8+
*
9+
* Given a string s, return the minimum number of characters you need to delete to make s good.
10+
*
11+
* The frequency of a character in a string is the number of times it appears in the string.
12+
* For example, in the string "aab", the frequency of 'a' is 2, while the frequency of 'b' is 1.
13+
*/
14+
15+
/**
16+
* @param {string} s
17+
* @return {number}
18+
*/
19+
var minDeletions = function(s) {
20+
const charFrequencies = new Array(26).fill(0);
21+
for (const char of s) {
22+
charFrequencies[char.charCodeAt(0) - 97]++;
23+
}
24+
25+
charFrequencies.sort((a, b) => b - a);
26+
let result = 0;
27+
const usedFrequencies = new Set();
28+
29+
for (const freq of charFrequencies) {
30+
if (freq === 0) break;
31+
let currentFreq = freq;
32+
while (usedFrequencies.has(currentFreq) && currentFreq > 0) {
33+
currentFreq--;
34+
result++;
35+
}
36+
usedFrequencies.add(currentFreq);
37+
}
38+
39+
return result;
40+
};

0 commit comments

Comments
 (0)