(LeetCode)Isomorphic Strings --- 同构字符串

来源:互联网 发布:金属期货软件 编辑:程序博客网 时间:2024/05/20 08:25

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.

Note:
You may assume both s and t have the same length.

Subscribe to see which companies asked this question


解题分析:

这里的意思和前几天刷的一道题目很像。就是判断字符串的结构,这里可以说判断构成,

举例:

egg 与 add, 这里存储 e 对应着 a,  g 对应着 d.然后再判断的时候发现有问题,对应不上,说明就不是同构的。

# -*- coding:utf-8 -*-__author__ = 'jiuzhang'class Solution(object):    def isIsomorphic(self, s, t):        s_list = list(s)        t_list = list(t)        if len(s) != len(t):            return False        sDict, tDict = {}, {}        for i, j in zip(s_list, t_list):            if i not in sDict:                sDict[i] = j            if j not in tDict:                tDict[j] = i            if tDict[j] != i or sDict[i] != j:                return False        return True



0 0
原创粉丝点击