Skip to content

Commit d8966d4

Browse files
authored
Create 2423-remove-letter-to-equalize-frequency.js
1 parent 8ba3703 commit d8966d4

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {string} word
3+
* @return {boolean}
4+
*/
5+
const equalFrequency = function (word) {
6+
const cnt = {}
7+
for(const ch of word) {
8+
if(cnt[ch] == null) cnt[ch] = 0
9+
cnt[ch]++
10+
}
11+
12+
for(const ch of word) {
13+
cnt[ch]--
14+
if(cnt[ch] === 0) delete cnt[ch]
15+
const s = new Set([...Object.values(cnt)])
16+
if(s.size === 1) return true
17+
18+
if(cnt[ch] == null) cnt[ch] = 0
19+
cnt[ch]++
20+
}
21+
22+
return false
23+
}

0 commit comments

Comments
 (0)