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..605e1b603 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,18 @@ package com.codedifferently.lesson13; -public class Lesson13 { +import java.util.HashMap; - /** - * Provide the solution to LeetCode 3146 here: - * https://leetcode.com/problems/permutation-difference-between-two-strings - */ +public class Lesson13 { public int findPermutationDifference(String s, String t) { - return 0; + var indexMap = new HashMap(); + for (int i = 0; i < t.length(); i++) { + indexMap.put(t.charAt(i), i); + } + + int result = 0; + for (int i = 0; i < s.length(); i++) { + result += Math.abs(i - indexMap.getOrDefault(s.charAt(i), -1)); + } + return result; } } diff --git a/lesson_13/maps_ts/src/lesson13.ts b/lesson_13/maps_ts/src/lesson13.ts index 5207487e2..b0d483fdf 100644 --- a/lesson_13/maps_ts/src/lesson13.ts +++ b/lesson_13/maps_ts/src/lesson13.ts @@ -1,7 +1,14 @@ -/** - * 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 indexMap = new Map(); + + for (let i = 0; i < t.length; i++) { + indexMap.set(t.charAt(i), i); + } + + let result = 0; + for (let i = 0; i < s.length; i++) { + result += Math.abs(i - (indexMap.get(s.charAt(i)) ?? -1)); + } + + return result; +} \ No newline at end of file