1. 程式人生 > >【leetcode 簡單】 第五十九題 同構字符串

【leetcode 簡單】 第五十九題 同構字符串

另一個 tco 保留 {} 簡單 urn code 映射 for

給定兩個字符串 s t,判斷它們是否是同構的。

如果 s 中的字符可以被替換得到 t ,那麽這兩個字符串是同構的。

所有出現的字符都必須用另一個字符替換,同時保留字符的順序。兩個字符不能映射到同一個字符上,但字符可以映射自己本身。

示例 1:

輸入: s = "egg", t = "add"
輸出: true

示例 2:

輸入: s = "foo", t = "bar"
輸出: false

示例 3:

輸入: s = "paper", t = "title"
輸出: true

說明:
你可以假設 s t 具有相同的長度。

class Solution:
    def
isIsomorphic(self, s, t): """ :type s: str :type t: str :rtype: bool """ a = {} if len(set(s)) != len(set(t)): return False for i in range(len(s)): if s[i] not in a: a[s[i]] = t[i] else
: if a[s[i]] != t[i]: return False return True

class Solution:
    def isIsomorphic(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        return len(set(zip(s,t))) == len(set(s)) == len(set(t))

【leetcode 簡單】 第五十九題 同構字符串