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..89f09c098 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; + HashMap hashMap = new HashMap<>(); + int permDifferences = 0; + for (int i = 0; i < s.length(); i++) { + hashMap.put(s.charAt(i), i); + } + for (char key : hashMap.keySet()) { + int indexInT = t.indexOf(Character.toString(key)); + permDifferences += Math.abs(s.indexOf(key) - indexInT); + } + return permDifferences; } } diff --git a/lesson_13/maps_ts/src/lesson13.ts b/lesson_13/maps_ts/src/lesson13.ts index 5207487e2..f9a6b9e8b 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 map = new Map(); + for (let i = 0; i < s.length; i++) { + map.set(s.charAt(i), i); + } + let permDifferences = 0; + for (const key of map.keys()) { + const indexInT = t.indexOf(String(key)); + permDifferences += Math.abs(s.indexOf(key) - indexInT); + } + return permDifferences; + } +