blob: 68178a193cb2c361182953d48447fd34cbde837f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# 205. Isomorphic Strings
"""
given two strings 's' and 't', determine if they are isomorphic. two strings
's' and 't' are isomorphic if the characters in 's' can be replaced to get
't'. all occurences of a character must be replaced with another character
while presevering the order of characters. no two characters may map to the
same character, but a character may map to itself.
"""
class Solution(object):
def isIsomorphic(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
s1, t1 = [0 for _ in range(256)], [0 for _ in range(256)]
for i in range(len(s)):
if s1[ord(s[i])] != t1[ord(t[i])]:
return False
s1[ord(s[i])] = i + 1
t1[ord(t[i])] = i + 1
return True
if __name__ == "__main__":
obj = Solution()
print(obj.isIsomorphic("egg", "add"))
print(obj.isIsomorphic("foo", "bar"))
print(obj.isIsomorphic("paper", "title"))
|