You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/0200-0299/0205.Isomorphic Strings/README_EN.md
+43-44
Original file line number
Diff line number
Diff line change
@@ -32,18 +32,25 @@
32
32
33
33
## Solutions
34
34
35
+
**Approach 1: Hash Table or Array**
36
+
37
+
We can use two hash tables or arrays $d_1$ and $d_2$ to record the character mapping relationship between $s$ and $t$.
38
+
39
+
Traverse $s$ and $t$, if the corresponding character mapping relationships in $d_1$ and $d_2$ are different, return `false`, otherwise update the corresponding character mapping relationships in $d_1$ and $d_2$. After the traversal is complete, it means that $s$ and $t$ are isomorphic, and return `true`.
40
+
41
+
The time complexity is $O(n)$ and the space complexity is $O(C)$. Where $n$ is the length of the string $s$; and $C$ is the size of the character set, which is $C = 256$ in this problem.
42
+
35
43
<!-- tabs:start -->
36
44
37
45
### **Python3**
38
46
39
47
```python
40
48
classSolution:
41
49
defisIsomorphic(self, s: str, t: str) -> bool:
42
-
d1, d2 = {}, {}
50
+
d1 = {}
51
+
d2 = {}
43
52
for a, b inzip(s, t):
44
-
if a in d1 and d1[a] != b:
45
-
returnFalse
46
-
if b in d2 and d2[b] != a:
53
+
if (a in d1 and d1[a] != b) or (b in d2 and d2[b] != a):
0 commit comments