Skip to content

Commit 85cb473

Browse files
committed
O(n) time and O(1) space using hashset.
1 parent 7832f46 commit 85cb473

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
Given two strings s and t, determine if they are isomorphic.
3+
4+
Two strings are isomorphic if the characters in s can be replaced to get t.
5+
6+
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.
7+
8+
Example 1:
9+
10+
Input: s = "egg", t = "add"
11+
Output: true
12+
Example 2:
13+
14+
Input: s = "foo", t = "bar"
15+
Output: false
16+
Example 3:
17+
18+
Input: s = "paper", t = "title"
19+
Output: true
20+
Note:
21+
You may assume both s and t have the same length.
22+
"""
23+
24+
25+
class Solution:
26+
def isIsomorphic(self, s: str, t: str) -> bool:
27+
hset = set()
28+
for i, j in zip(s, t):
29+
if not (i, j) in hset:
30+
hset.add((i, j))
31+
return len(hset) == len(set(s)) == len(set(t))

0 commit comments

Comments
 (0)