标签:
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