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 0c981abb..a6eac947 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,5 +1,7 @@ package com.codedifferently.lesson13; +import java.util.HashMap; + public class Lesson13 { /** @@ -7,6 +9,15 @@ public class Lesson13 { * https://leetcode.com/problems/permutation-difference-between-two-strings */ public int findPermutationDifference(String s, String t) { - return 0; + var sIndexByChar = new HashMap(); + int permDiff = 0; + for (int i = 0; i < s.length(); i++) { + sIndexByChar.put(s.charAt(i), i); + } + for (char key : sIndexByChar.keySet()) { + int indexInT = t.indexOf(Character.toString(key)); + permDiff += Math.abs(s.indexOf(key) - indexInT); + } + return permDiff; } } diff --git a/lesson_13/maps_ts/src/lesson13.ts b/lesson_13/maps_ts/src/lesson13.ts index 5207487e..6ee168f9 100644 --- a/lesson_13/maps_ts/src/lesson13.ts +++ b/lesson_13/maps_ts/src/lesson13.ts @@ -3,5 +3,14 @@ * https://leetcode.com/problems/permutation-difference-between-two-strings */ export function findPermutationDifference(s: string, t: string): number { - return 0; + const sIndexByChar = new Map(); + let permDiff = 0; + for (let i = 0; i < s.length; i++) { + sIndexByChar.set(s.charAt(i), i); + } + for (const key of sIndexByChar.keys()) { + const indexInT = t.indexOf(key); + permDiff += Math.abs(s.indexOf(key) - indexInT); + } + return permDiff; }