-
Notifications
You must be signed in to change notification settings - Fork 0
/
205_IsomorphicStrings.py
71 lines (65 loc) · 2.02 KB
/
205_IsomorphicStrings.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
class Solution:
# @param {string} s
# @param {string} t
# @return {boolean}
def isIsomorphic(self, s, t):
if len(s) != len(t):
return False
if len(s) == 0:
return False
chmap = {}
chmap[s[0]] = t[0]
for idx in range(1, len(s)):
if s[idx] == s[idx-1]:
if t[idx] != t[idx-1]:
return False
else:
if t[idx] == t[idx-1]:
return False
else:
if s[idx] in chmap:
if t[idx] != chmap[s[idx]]:
return False
else:
chmap[s[idx]] = t[idx]
return True
def aisIsomorphic(self, s, t):
if len(s) != len(t):
return False
if len(s) == 0:
return False
rla = self.runLength(s)
rlb = self.runLength(t)
if len(rla) != len(rlb):
return False
else:
chmap = {}
for idx in range(len(rla)):
ach, acount = rla[idx]
bch, bcount = rlb[idx]
if acount != bcount:
return False
else:
if ach not in chmap:
chmap[ach] = bch
else:
if bch != chmap[ach]:
return False
return True
def runLength(self, s):
result = [[s[0], 1]]
for idx in range(1, len(s)):
if s[idx] == s[idx-1]:
result[-1][1] += 1
else:
result.append([s[idx], 1])
return result
if __name__ == "__main__":
sol = Solution()
print sol.runLength("abc")
print sol.runLength("egg")
print sol.isIsomorphic("abc", "def")
print sol.isIsomorphic("foo", "bar")
print sol.isIsomorphic("foo", "egg")
print sol.isIsomorphic("paper", "title")
print sol.isIsomorphic("paper", "tidle")