Skip to content
LC-3146 Easy LeetCode

3146. Permutation Difference between Two Strings

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 87% Topics: Hash Table, String
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n + 26)
# Space: O(26)

# hash table
class Solution(object):
    def findPermutationDifference(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: int
        """
        lookup = [-1]*26
        for i, x in enumerate(s):
            lookup[ord(x)-ord('a')] = i
        return sum(abs(lookup[ord(x)-ord('a')]-i)for i, x in enumerate(t))

Solution from kamyu104/LeetCode-Solutions · MIT

Similar questions