Skip to content

Commit 62ebeab

Browse files
authored
Create 1647-minimum-deletions-to-make-character-frequencies-unique.js
1 parent e650410 commit 62ebeab

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Hasing
3+
* Time O(n) | Space O(n)
4+
* https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/
5+
* @param {string} s
6+
* @return {number}
7+
*/
8+
var minDeletions = function(s) {
9+
10+
// hash frequency
11+
let i = 0;
12+
const charHash = new Map();
13+
while(i < s.length) {
14+
const frequency = charHash.get(s[i]) || 0;
15+
charHash.set(s[i], frequency+1);
16+
i++;
17+
}
18+
const frequencyHash = new Map();
19+
for(const [key, val] of charHash) {
20+
const frequency = frequencyHash.get(val) || 0;
21+
frequencyHash.set(val, frequency+1);
22+
}
23+
24+
let min = 0;
25+
for(const [key, val] of frequencyHash) {
26+
let frequency = key;
27+
let frequencyOfFrequency = val;
28+
while(frequencyOfFrequency > 1) {
29+
while(frequencyHash.has(frequency)) {
30+
frequency -= 1;
31+
min += 1;
32+
}
33+
if(frequency > 0) frequencyHash.set(frequency, 1);
34+
frequency = key;
35+
frequencyOfFrequency -= 1;
36+
}
37+
}
38+
return min;
39+
};

0 commit comments

Comments
 (0)