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 Reading material
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