forked from wuduhren/leetcode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisomorphic-strings.py
More file actions
33 lines (25 loc) · 1.11 KB
/
isomorphic-strings.py
File metadata and controls
33 lines (25 loc) · 1.11 KB
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
33
"""
The description of the problem is unclear, let me rephrase it.
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 and t can be replaced to get s.
For each replacement, the characters in the same string must be replaced with another character while preserving the order of characters.
No two characters in the same string may map to the same character, but a character may map to itself.
Two replacement are independent from each other. In other words, s -> t and t -> s does not affect each other.
"""
"""
Time: O(N)
Space: O(N)
"""
class Solution(object):
def isIsomorphic(self, s, t):
if len(s)!=len(t): return False
# check if s1 chars could be replaced and become s2
def helper(s1, s2):
memo = {}
for i in xrange(len(s)):
c1 = s1[i]
c2 = s2[i]
if c1 in memo and memo[c1]!=c2: return False
memo[c1] = c2
return True
return helper(s, t) and helper(t, s)