2025-08-22 17:25:28 +0000 UTC

Permutation Difference between Two Strings

Code

class Solution:
    def findPermutationDifference(self, s: str, t: str) -> int:
        pos = [0] * 26
        for i in range(len(s)):
            pos[ord(s[i]) - 97] += i
        res = 0
        for i in range(len(t)):
            res += abs(i - pos[ord(t[i]) - 97])
        return res