[Leetcode]3146. Permutation Difference between Two Strings

2024. 7. 30. 11:52java/javaAlgorithm

1. problem :

https://leetcode.com/problems/permutation-difference-between-two-strings/description/

 

2. solution 1 :

public class Solution {
    public int findPermutationDifference(String s, String t) {
        int count = 0;
        HashMap<Character, Integer> hashMap = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            hashMap.put(s.charAt(i), i);
        }
        int totalAbsValue = 0;

        for (int i = 0; i < t.length(); i++) {
            char c = t.charAt(i);
            int indexInS = hashMap.get(c);
            totalAbsValue += Math.abs(i -indexInS);
        }
        return totalAbsValue;
    }
}