Skip to content

Latest commit

 

History

History
52 lines (44 loc) · 1.21 KB

0205._isomorphic_strings.md

File metadata and controls

52 lines (44 loc) · 1.21 KB

Navigation

Links:

  1. https://leetcode.com/problems/isomorphic-strings
  2. https://leetcode-cn.com/problems/isomorphic-strings/

Solution 1

class Solution:
    def isIsomorphic(self, s, t):
        return len(set(zip(s, t))) == len(set(s)) == len(set(t))

Solution 2 index的对比

同构代表两个字符串中,每个位置上字符在自身第一次出现的索引相同

class Solution:
    def isIsomorphic(self, s, t):
        return [s.find(i) for i in s] == [t.find(i) for i in t]

class Solution:
    def isIsomorphic(self, s, t):
        return [s.index(i) for i in s] == [t.index(i) for i in t]

class Solution:
    def isIsomorphic(self, s, t):
        return map(s.find, s) == map(t.find, t)

class Solution:
    def isIsomorphic(self, s, t):
        d1, d2 = {}, {}

        for i, val in enumerate(s):
            d1[val] = d1.get(val, []) + [i]

        for i, val in enumerate(t):
            d2[val] = d2.get(val, []) + [i]

        return sorted(d1.values()) == sorted(d2.values())