From 1a3108f16850d713b1a4b87d51f0273570abe293 Mon Sep 17 00:00:00 2001 From: dxsmith244 Date: Thu, 24 Oct 2024 19:02:53 +0000 Subject: [PATCH] feat: Adds David Smith Lesson 13 leetcode 3146 Typescript and Java --- .../com/codedifferently/lesson13/Lesson13.java | 17 +++++++++++++++-- lesson_13/maps_ts/src/lesson13.ts | 15 ++++++++++++++- 2 files changed, 29 insertions(+), 3 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 0c981abb..e3e6c437 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,5 @@ package com.codedifferently.lesson13; - +import java.util.HashMap; public class Lesson13 { /** @@ -7,6 +7,19 @@ public class Lesson13 { * https://leetcode.com/problems/permutation-difference-between-two-strings */ public int findPermutationDifference(String s, String t) { - return 0; + int difference=0; + HashMap indexMap=new HashMap<>(); + + for (int i = 0; i < s.length(); i++) { + indexMap.put(s.charAt(i), i); + } + + for (int i = 0; i < t.length(); i++) { + char ch = t.charAt(i); + int indexInS = indexMap.get(ch); + difference += Math.abs(indexInS - i); + } + + return difference; } } diff --git a/lesson_13/maps_ts/src/lesson13.ts b/lesson_13/maps_ts/src/lesson13.ts index 5207487e..ef4db118 100644 --- a/lesson_13/maps_ts/src/lesson13.ts +++ b/lesson_13/maps_ts/src/lesson13.ts @@ -3,5 +3,18 @@ * https://leetcode.com/problems/permutation-difference-between-two-strings */ export function findPermutationDifference(s: string, t: string): number { - return 0; + let difference = 0; + const indexMap = new Map(); + + for (let i = 0; i < s.length; i++) { + indexMap.set(s[i], i); + } + + for (let i = 0; i < t.length; i++) { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const indexInS = indexMap.get(t[i])!; + difference += Math.abs(indexInS - i); + } + + return difference; }