Skip to content

Commit cbad88a

Browse files
committed
feat: add ts solution to lc problem: No.2423
No.2423.Remove Letter To Equalize Frequency
1 parent fd9c8e5 commit cbad88a

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

solution/2400-2499/2423.Remove Letter To Equalize Frequency/README.md

+20-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,26 @@ func equalFrequency(word string) bool {
153153
### **TypeScript**
154154

155155
```ts
156-
156+
function equalFrequency(word: string): boolean {
157+
const map = new Map();
158+
for (const c of word) {
159+
map.set(c, (map.get(c) ?? 0) + 1);
160+
}
161+
const count = new Map();
162+
for (const v of map.values()) {
163+
count.set(v, (count.get(v) ?? 0) + 1);
164+
}
165+
if (count.size === 1) {
166+
return map.size == 1 || [...count.keys()][0] === 1;
167+
}
168+
if (count.size === 2) {
169+
return [...count.entries()].some(
170+
(v, i, arr) =>
171+
(v[0] === 1 || v[0] - arr[i ^ 1][0] === 1) && v[1] === 1,
172+
);
173+
}
174+
return false;
175+
}
157176
```
158177

159178
### **...**

solution/2400-2499/2423.Remove Letter To Equalize Frequency/README_EN.md

+20-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,26 @@ func equalFrequency(word string) bool {
139139
### **TypeScript**
140140

141141
```ts
142-
142+
function equalFrequency(word: string): boolean {
143+
const map = new Map();
144+
for (const c of word) {
145+
map.set(c, (map.get(c) ?? 0) + 1);
146+
}
147+
const count = new Map();
148+
for (const v of map.values()) {
149+
count.set(v, (count.get(v) ?? 0) + 1);
150+
}
151+
if (count.size === 1) {
152+
return map.size == 1 || [...count.keys()][0] === 1;
153+
}
154+
if (count.size === 2) {
155+
return [...count.entries()].some(
156+
(v, i, arr) =>
157+
(v[0] === 1 || v[0] - arr[i ^ 1][0] === 1) && v[1] === 1,
158+
);
159+
}
160+
return false;
161+
}
143162
```
144163

145164
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function equalFrequency(word: string): boolean {
2+
const map = new Map();
3+
for (const c of word) {
4+
map.set(c, (map.get(c) ?? 0) + 1);
5+
}
6+
const count = new Map();
7+
for (const v of map.values()) {
8+
count.set(v, (count.get(v) ?? 0) + 1);
9+
}
10+
if (count.size === 1) {
11+
return map.size == 1 || [...count.keys()][0] === 1;
12+
}
13+
if (count.size === 2) {
14+
return [...count.entries()].some(
15+
(v, i, arr) =>
16+
(v[0] === 1 || v[0] - arr[i ^ 1][0] === 1) && v[1] === 1,
17+
);
18+
}
19+
return false;
20+
}

0 commit comments

Comments
 (0)