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..411e2e9ca 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,23 @@ 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; + var charIndexMap = new HashMap(); + + int totalDifference = 0; + + for (int sIndex = 0; sIndex < s.length(); sIndex++) { + charIndexMap.put(s.charAt(sIndex), sIndex); + } + + for (int tIndex = 0; tIndex < t.length(); tIndex++) { + int indexInS = charIndexMap.get(t.charAt(tIndex)); + totalDifference += Math.abs(indexInS - tIndex); + } + + return totalDifference; } } diff --git a/lesson_13/maps_ts/src/lesson13.ts b/lesson_13/maps_ts/src/lesson13.ts index 5207487e2..b75d9213d 100644 --- a/lesson_13/maps_ts/src/lesson13.ts +++ b/lesson_13/maps_ts/src/lesson13.ts @@ -1,7 +1,19 @@ -/** - * 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 charPositionMap = new Map(); + + let totalDifference = 0; + + for (let sIndex = 0; sIndex < s.length; sIndex++) { + charPositionMap.set(s.charAt(sIndex), sIndex) + } + + for (let tIndex = 0; tIndex < t.length; tIndex++) { + const indexInS = charPositionMap.get(t.charAt(tIndex)); + + if (indexInS != undefined) { + totalDifference += Math.abs(indexInS - tIndex); + } + } + + return totalDifference; }