Skip to content

Commit b3ec7aa

Browse files
author
jjcapparell
committed
fix: simplified code and made shorter
1 parent c22c3d0 commit b3ec7aa

File tree

1 file changed

+6
-14
lines changed

1 file changed

+6
-14
lines changed

lesson_13/maps_ts/src/lesson13.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,17 @@
11
export function findPermutationDifference(s: string, t: string): number {
22
const stringS = new Map<string, number>();
3-
for (let i = 0; i < s.length; i++) {
4-
stringS.set(s.charAt(i), i);
5-
}
6-
73
const stringT = new Map<string, number>();
4+
let diffSum = 0;
85
for (let i = 0; i < t.length; i++) {
6+
stringS.set(s.charAt(i), i);
97
stringT.set(t.charAt(i), i);
108
}
11-
12-
return compareMaps(stringS, stringT);
13-
}
14-
15-
function compareMaps(map1: Map<string, number>, map2: Map<string, number>): number {
16-
let diffSum = 0;
17-
for (const key of map1.keys()) {
18-
if (map2.has(key)) {
9+
for (const key of stringS.keys()) {
10+
if (stringT.has(key)) {
1911
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
20-
const value1 = map1.get(key)!; // Non-null assertion because we checked with has()
12+
const value1 = stringS.get(key)!; // Non-null assertion because we checked with has()
2113
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
22-
const value2 = map2.get(key)!; // Non-null assertion
14+
const value2 = stringT.get(key)!; // Non-null assertion
2315
const difference = Math.abs(value1 - value2);
2416
diffSum += difference;
2517
}

0 commit comments

Comments
 (0)