码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-08-18 10:11:06      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

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





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

标签:

原文地址:http://blog.csdn.net/u012965373/article/details/52238067

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!