-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path205_isomorphic_strings.py
More file actions
35 lines (33 loc) · 972 Bytes
/
Copy path205_isomorphic_strings.py
File metadata and controls
35 lines (33 loc) · 972 Bytes
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
from collections import defaultdict
class Solution(object):
def isIsomorphic(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
if len(s) != len(t):
return False
s2t = defaultdict(str)
t2s = defaultdict(str)
for i in range(len(s)):
if s[i] in s2t:
if s2t[s[i]] != t[i]:
return False
elif t[i] in t2s:
return False
else:
s2t[s[i]] = t[i]
t2s[t[i]] = s[i]
return True
vectors = [
['egg', 'add'], True,
['foo', 'bar'], False,
['paper', 'title'], True
]
for i in range(0, len(vectors), 2):
params = vectors[i]
expected = vectors[i + 1]
print(f'{params} {expected}')
returned = Solution().isIsomorphic(*params)
assert expected == returned, f'for {params} expected {expected}, returned {returned}!'