[코테 풀이] Permutation Difference between Two Strings

시내·2024년 6월 10일
0

Q_3146) Permutation Difference between Two Strings

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

You are given two strings s and t such that every character occurs at most once in s and t is a permutation of s.

The permutation difference between s and t is defined as the sum of the absolute difference between the index of the occurrence of each character in s and the index of the occurrence of the same character in t.

Return the permutation difference between s and t.

class Solution {
    public int findPermutationDifference(String s, String t) {
        int res = 0;
        for (int a = 0; a < s.length(); a++) {
            res += Math.abs(a - t.indexOf(s.charAt(a)));
        }
        return res;
    }
}
profile
contact 📨 ksw08215@gmail.com

0개의 댓글