LeetCode----Isomorphic Strings

Isomorphic Strings

Given two strings s and t, determine if they are isomorphic.

Two strings are isomorphic if the characters in s can be replaced to get t.

All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.

For example,
Given "egg""add", return true.

Given "foo""bar", return false.

Given "paper""title", return true.


分析:

跟Word Pattern一样的题,一样的思路。


代码:

class Solution(object):
    def isIsomorphic(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        wd = dict()
        s_l = len(s)
        s_t = len(t)
        if s_l != s_t:
            return False
        wd = dict(map(lambda a: (a, None), list(s)))
        for i in range(s_l):
            if wd[s[i]] is None:
                wd[s[i]] = t[i]
            else:
                if wd[s[i]] != t[i]:
                    return False
        ds = set()
        for i in wd:
            ds.add(wd[i])
        if len(ds) != len(wd):
            return False
        return True

你可能感兴趣的:(LeetCode,python,isomorphic,strings)