From 5629699769a9e20de1b0ce108d5adf19725cf1b6 Mon Sep 17 00:00:00 2001 From: jjcapparell <“jjcapparell244@gmail.com> Date: Thu, 24 Oct 2024 14:36:59 +0000 Subject: [PATCH 1/9] Feat: added content to Lesson13.java --- .../codedifferently/lesson13/Lesson13.java | 30 +++++++++++++++---- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/lesson_13/maps_java/maps_app/src/main/java/com/codedifferently/lesson13/Lesson13.java b/lesson_13/maps_java/maps_app/src/main/java/com/codedifferently/lesson13/Lesson13.java index 0c981abbf..323c8639e 100644 --- a/lesson_13/maps_java/maps_app/src/main/java/com/codedifferently/lesson13/Lesson13.java +++ b/lesson_13/maps_java/maps_app/src/main/java/com/codedifferently/lesson13/Lesson13.java @@ -1,12 +1,30 @@ package com.codedifferently.lesson13; - +import java.util.HashMap; public class Lesson13 { - /** - * Provide the solution to LeetCode 3146 here: - * https://leetcode.com/problems/permutation-difference-between-two-strings - */ public int findPermutationDifference(String s, String t) { - return 0; + HashMap stringS = new HashMap<>(); + for (int i=0; i stringT = new HashMap<>(); + for (int i=0; i map1, HashMap map2) { + int diffSum=0; + for (Character key : map1.keySet()) { + if (map2.containsKey(key)) { + int value1 = map1.get(key); + int value2 = map2.get(key); + int difference = Math.abs(value1 - value2); + diffSum+=difference; + } + } + return diffSum; } } From 29cc80cb3fcc6dd46f939a8de1978b992ffbd35b Mon Sep 17 00:00:00 2001 From: jjcapparell <“jjcapparell244@gmail.com> Date: Thu, 24 Oct 2024 15:02:41 +0000 Subject: [PATCH 2/9] fix: gradle command edit --- .../java/com/codedifferently/lesson13/Lesson13.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lesson_13/maps_java/maps_app/src/main/java/com/codedifferently/lesson13/Lesson13.java b/lesson_13/maps_java/maps_app/src/main/java/com/codedifferently/lesson13/Lesson13.java index 323c8639e..2a514881e 100644 --- a/lesson_13/maps_java/maps_app/src/main/java/com/codedifferently/lesson13/Lesson13.java +++ b/lesson_13/maps_java/maps_app/src/main/java/com/codedifferently/lesson13/Lesson13.java @@ -1,28 +1,29 @@ package com.codedifferently.lesson13; + import java.util.HashMap; + public class Lesson13 { public int findPermutationDifference(String s, String t) { HashMap stringS = new HashMap<>(); - for (int i=0; i stringT = new HashMap<>(); - for (int i=0; i map1, HashMap map2) { - int diffSum=0; + int diffSum = 0; for (Character key : map1.keySet()) { if (map2.containsKey(key)) { int value1 = map1.get(key); int value2 = map2.get(key); int difference = Math.abs(value1 - value2); - diffSum+=difference; + diffSum += difference; } } return diffSum; From a1637c4c110c68fba74bde444cd5102f55bcb740 Mon Sep 17 00:00:00 2001 From: jjcapparell <“jjcapparell244@gmail.com> Date: Thu, 24 Oct 2024 15:15:59 +0000 Subject: [PATCH 3/9] Feat: added content to lesson13.ts --- lesson_13/maps_ts/src/lesson13.ts | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/lesson_13/maps_ts/src/lesson13.ts b/lesson_13/maps_ts/src/lesson13.ts index 5207487e2..e2f6548c0 100644 --- a/lesson_13/maps_ts/src/lesson13.ts +++ b/lesson_13/maps_ts/src/lesson13.ts @@ -1,7 +1,26 @@ -/** - * Provide the solution to LeetCode 3146 here: - * https://leetcode.com/problems/permutation-difference-between-two-strings - */ export function findPermutationDifference(s: string, t: string): number { - return 0; + const stringS: Map = new Map(); + for (let i = 0; i < s.length; i++) { + stringS.set(s.charAt(i), i); + } + + const stringT: Map = new Map(); + for (let i = 0; i < t.length; i++) { + stringT.set(t.charAt(i), i); + } + + return compareMaps(stringS, stringT); } + +function compareMaps(map1: Map, map2: Map): number { + let diffSum = 0; + for (const key of map1.keys()) { + if (map2.has(key)) { + const value1 = map1.get(key)!; // Non-null assertion because we checked with has() + const value2 = map2.get(key)!; // Non-null assertion + const difference = Math.abs(value1 - value2); + diffSum += difference; + } + } + return diffSum; +} \ No newline at end of file From c22c3d04feef961402ba21bc06abb4b78830e731 Mon Sep 17 00:00:00 2001 From: jjcapparell <“jjcapparell244@gmail.com> Date: Thu, 24 Oct 2024 15:49:41 +0000 Subject: [PATCH 4/9] fix: lesson13.ts edits to pass npm testing --- lesson_13/maps_ts/src/lesson13.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lesson_13/maps_ts/src/lesson13.ts b/lesson_13/maps_ts/src/lesson13.ts index e2f6548c0..818f0b39b 100644 --- a/lesson_13/maps_ts/src/lesson13.ts +++ b/lesson_13/maps_ts/src/lesson13.ts @@ -1,10 +1,10 @@ export function findPermutationDifference(s: string, t: string): number { - const stringS: Map = new Map(); + const stringS = new Map(); for (let i = 0; i < s.length; i++) { stringS.set(s.charAt(i), i); } - const stringT: Map = new Map(); + const stringT = new Map(); for (let i = 0; i < t.length; i++) { stringT.set(t.charAt(i), i); } @@ -16,7 +16,9 @@ function compareMaps(map1: Map, map2: Map): numb let diffSum = 0; for (const key of map1.keys()) { if (map2.has(key)) { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const value1 = map1.get(key)!; // Non-null assertion because we checked with has() + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const value2 = map2.get(key)!; // Non-null assertion const difference = Math.abs(value1 - value2); diffSum += difference; From b3ec7aa17d13c82da0b4e2fa4be25fa2b178eff5 Mon Sep 17 00:00:00 2001 From: jjcapparell <“jjcapparell244@gmail.com> Date: Thu, 24 Oct 2024 17:12:25 +0000 Subject: [PATCH 5/9] fix: simplified code and made shorter --- lesson_13/maps_ts/src/lesson13.ts | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/lesson_13/maps_ts/src/lesson13.ts b/lesson_13/maps_ts/src/lesson13.ts index 818f0b39b..76c9897de 100644 --- a/lesson_13/maps_ts/src/lesson13.ts +++ b/lesson_13/maps_ts/src/lesson13.ts @@ -1,25 +1,17 @@ export function findPermutationDifference(s: string, t: string): number { const stringS = new Map(); - for (let i = 0; i < s.length; i++) { - stringS.set(s.charAt(i), i); - } - const stringT = new Map(); + let diffSum = 0; for (let i = 0; i < t.length; i++) { + stringS.set(s.charAt(i), i); stringT.set(t.charAt(i), i); } - - return compareMaps(stringS, stringT); -} - -function compareMaps(map1: Map, map2: Map): number { - let diffSum = 0; - for (const key of map1.keys()) { - if (map2.has(key)) { + for (const key of stringS.keys()) { + if (stringT.has(key)) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const value1 = map1.get(key)!; // Non-null assertion because we checked with has() + const value1 = stringS.get(key)!; // Non-null assertion because we checked with has() // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const value2 = map2.get(key)!; // Non-null assertion + const value2 = stringT.get(key)!; // Non-null assertion const difference = Math.abs(value1 - value2); diffSum += difference; } From 1ebdea82edac42f064e3d5ddac9364111deb26ff Mon Sep 17 00:00:00 2001 From: jjcapparell <“jjcapparell244@gmail.com> Date: Thu, 24 Oct 2024 17:17:59 +0000 Subject: [PATCH 6/9] fix: simplified code and made shorter (java) --- .../com/codedifferently/lesson13/Lesson13.java | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/lesson_13/maps_java/maps_app/src/main/java/com/codedifferently/lesson13/Lesson13.java b/lesson_13/maps_java/maps_app/src/main/java/com/codedifferently/lesson13/Lesson13.java index 2a514881e..f6709002d 100644 --- a/lesson_13/maps_java/maps_app/src/main/java/com/codedifferently/lesson13/Lesson13.java +++ b/lesson_13/maps_java/maps_app/src/main/java/com/codedifferently/lesson13/Lesson13.java @@ -6,22 +6,16 @@ public class Lesson13 { public int findPermutationDifference(String s, String t) { HashMap stringS = new HashMap<>(); - for (int i = 0; i < s.length(); i++) { - stringS.put(s.charAt(i), i); - } HashMap stringT = new HashMap<>(); + int diffSum = 0; for (int i = 0; i < t.length(); i++) { + stringS.put(s.charAt(i), i); stringT.put(t.charAt(i), i); } - return compareMaps(stringS, stringT); - } - - public int compareMaps(HashMap map1, HashMap map2) { - int diffSum = 0; - for (Character key : map1.keySet()) { - if (map2.containsKey(key)) { - int value1 = map1.get(key); - int value2 = map2.get(key); + for (Character key : stringS.keySet()) { + if (stringT.containsKey(key)) { + int value1 = stringS.get(key); + int value2 = stringT.get(key); int difference = Math.abs(value1 - value2); diffSum += difference; } From 4e0fee1aa39914ea0a5610e50459c93c63043647 Mon Sep 17 00:00:00 2001 From: jjcapparell <“jjcapparell244@gmail.com> Date: Fri, 25 Oct 2024 13:36:31 +0000 Subject: [PATCH 7/9] chore: removed unnecessary lines of code for java and ts file --- .../java/com/codedifferently/lesson13/Lesson13.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lesson_13/maps_java/maps_app/src/main/java/com/codedifferently/lesson13/Lesson13.java b/lesson_13/maps_java/maps_app/src/main/java/com/codedifferently/lesson13/Lesson13.java index f6709002d..1a9ed43d6 100644 --- a/lesson_13/maps_java/maps_app/src/main/java/com/codedifferently/lesson13/Lesson13.java +++ b/lesson_13/maps_java/maps_app/src/main/java/com/codedifferently/lesson13/Lesson13.java @@ -13,12 +13,10 @@ public int findPermutationDifference(String s, String t) { stringT.put(t.charAt(i), i); } for (Character key : stringS.keySet()) { - if (stringT.containsKey(key)) { - int value1 = stringS.get(key); - int value2 = stringT.get(key); - int difference = Math.abs(value1 - value2); - diffSum += difference; - } + int value1 = stringS.get(key); + int value2 = stringT.get(key); + int difference = Math.abs(value1 - value2); + diffSum += difference; } return diffSum; } From 5ac42718e333d1925f934f87948c6e6ce3b6cf53 Mon Sep 17 00:00:00 2001 From: jjcapparell <“jjcapparell244@gmail.com> Date: Fri, 25 Oct 2024 18:40:34 +0000 Subject: [PATCH 8/9] chore: removing Lines for java file --- lesson_13/maps_ts/src/lesson13.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/lesson_13/maps_ts/src/lesson13.ts b/lesson_13/maps_ts/src/lesson13.ts index 76c9897de..59b68e454 100644 --- a/lesson_13/maps_ts/src/lesson13.ts +++ b/lesson_13/maps_ts/src/lesson13.ts @@ -8,9 +8,7 @@ export function findPermutationDifference(s: string, t: string): number { } for (const key of stringS.keys()) { if (stringT.has(key)) { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const value1 = stringS.get(key)!; // Non-null assertion because we checked with has() - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const value2 = stringT.get(key)!; // Non-null assertion const difference = Math.abs(value1 - value2); diffSum += difference; From 3d439d30728176dcc207b04bdd4d8d218c1aa693 Mon Sep 17 00:00:00 2001 From: jjcapparell <“jjcapparell244@gmail.com> Date: Fri, 25 Oct 2024 18:42:41 +0000 Subject: [PATCH 9/9] chore: line deletions for java file --- lesson_13/maps_ts/src/lesson13.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lesson_13/maps_ts/src/lesson13.ts b/lesson_13/maps_ts/src/lesson13.ts index 59b68e454..10503cb94 100644 --- a/lesson_13/maps_ts/src/lesson13.ts +++ b/lesson_13/maps_ts/src/lesson13.ts @@ -9,7 +9,7 @@ export function findPermutationDifference(s: string, t: string): number { for (const key of stringS.keys()) { if (stringT.has(key)) { const value1 = stringS.get(key)!; // Non-null assertion because we checked with has() - const value2 = stringT.get(key)!; // Non-null assertion + const value2 = stringT.get(key)!; // Non-null assertion const difference = Math.abs(value1 - value2); diffSum += difference; }