Skip to content

Commit

Permalink
feat: add ts solution to lcci problem: No.17.07
Browse files Browse the repository at this point in the history
No.17.07.Baby Names
  • Loading branch information
YangFong committed Apr 1, 2022
1 parent af6c87f commit 0e53cdc
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
33 changes: 33 additions & 0 deletions lcci/17.07.Baby Names/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,39 @@ class Solution {
}
```

### **TypeScript**

```ts
function trulyMostPopular(names: string[], synonyms: string[]): string[] {
const map = new Map<string, string>();
for (const synonym of synonyms) {
const [k1, k2] = [...synonym]
.slice(1, synonym.length - 1)
.join('')
.split(',');
const [v1, v2] = [map.get(k1) ?? k1, map.get(k2) ?? k2];
const min = v1 < v2 ? v1 : v2;
const max = v1 < v2 ? v2 : v1;
map.set(k1, min);
map.set(k2, min);
for (const [k, v] of map.entries()) {
if (v === max) {
map.set(k, min);
}
}
}

const keyCount = new Map<string, number>();
for (const name of names) {
const num = name.match(/\d+/)[0];
const k = name.split('(')[0];
const key = map.get(k) ?? k;
keyCount.set(key, (keyCount.get(key) ?? 0) + Number(num));
}
return [...keyCount.entries()].map(([k, v]) => `${k}(${v})`);
}
```

### **...**

```
Expand Down
33 changes: 33 additions & 0 deletions lcci/17.07.Baby Names/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,39 @@ class Solution {
}
```

### **TypeScript**

```ts
function trulyMostPopular(names: string[], synonyms: string[]): string[] {
const map = new Map<string, string>();
for (const synonym of synonyms) {
const [k1, k2] = [...synonym]
.slice(1, synonym.length - 1)
.join('')
.split(',');
const [v1, v2] = [map.get(k1) ?? k1, map.get(k2) ?? k2];
const min = v1 < v2 ? v1 : v2;
const max = v1 < v2 ? v2 : v1;
map.set(k1, min);
map.set(k2, min);
for (const [k, v] of map.entries()) {
if (v === max) {
map.set(k, min);
}
}
}

const keyCount = new Map<string, number>();
for (const name of names) {
const num = name.match(/\d+/)[0];
const k = name.split('(')[0];
const key = map.get(k) ?? k;
keyCount.set(key, (keyCount.get(key) ?? 0) + Number(num));
}
return [...keyCount.entries()].map(([k, v]) => `${k}(${v})`);
}
```

### **...**

```
Expand Down
28 changes: 28 additions & 0 deletions lcci/17.07.Baby Names/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function trulyMostPopular(names: string[], synonyms: string[]): string[] {
const map = new Map<string, string>();
for (const synonym of synonyms) {
const [k1, k2] = [...synonym]
.slice(1, synonym.length - 1)
.join('')
.split(',');
const [v1, v2] = [map.get(k1) ?? k1, map.get(k2) ?? k2];
const min = v1 < v2 ? v1 : v2;
const max = v1 < v2 ? v2 : v1;
map.set(k1, min);
map.set(k2, min);
for (const [k, v] of map.entries()) {
if (v === max) {
map.set(k, min);
}
}
}

const keyCount = new Map<string, number>();
for (const name of names) {
const num = name.match(/\d+/)[0];
const k = name.split('(')[0];
const key = map.get(k) ?? k;
keyCount.set(key, (keyCount.get(key) ?? 0) + Number(num));
}
return [...keyCount.entries()].map(([k, v]) => `${k}(${v})`);
}

0 comments on commit 0e53cdc

Please sign in to comment.