Skip to content

Commit d4bc818

Browse files
committed
feat: add solutions to lc problem: No.0205
No.0205.Isomorphic Strings
1 parent e2ef49d commit d4bc818

File tree

7 files changed

+117
-129
lines changed

7 files changed

+117
-129
lines changed

solution/0200-0299/0205.Isomorphic Strings/README.md

+42-45
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52-
**方法一:哈希表**
52+
**方法一:哈希表或数组**
53+
54+
我们可以用两个哈希表或数组 $d_1$ 和 $d_2$ 记录 $s$ 和 $t$ 中字符的映射关系。
55+
56+
遍历 $s$ 和 $t$,如果 $d_1$ 和 $d_2$ 中对应的字符映射关系不同,则返回 `false`,否则更新 $d_1$ 和 $d_2$ 中对应的字符映射关系。遍历结束,说明 $s$ 和 $t$ 是同构的,返回 `true`
57+
58+
时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 为字符串 $s$ 的长度;而 $C$ 为字符集大小,本题中 $C = 256$。
5359

5460
<!-- tabs:start -->
5561

@@ -60,11 +66,10 @@
6066
```python
6167
class Solution:
6268
def isIsomorphic(self, s: str, t: str) -> bool:
63-
d1, d2 = {}, {}
69+
d1 = {}
70+
d2 = {}
6471
for a, b in zip(s, t):
65-
if a in d1 and d1[a] != b:
66-
return False
67-
if b in d2 and d2[b] != a:
72+
if (a in d1 and d1[a] != b) or (b in d2 and d2[b] != a):
6873
return False
6974
d1[a] = b
7075
d2[b] = a
@@ -75,11 +80,11 @@ class Solution:
7580
class Solution:
7681
def isIsomorphic(self, s: str, t: str) -> bool:
7782
d1, d2 = [0] * 256, [0] * 256
78-
for i, (a, b) in enumerate(zip(s, t)):
83+
for i, (a, b) in enumerate(zip(s, t), 1):
7984
a, b = ord(a), ord(b)
8085
if d1[a] != d2[b]:
8186
return False
82-
d1[a] = d2[b] = i + 1
87+
d1[a] = d2[b] = i
8388
return True
8489
```
8590

@@ -134,12 +139,14 @@ class Solution {
134139
class Solution {
135140
public:
136141
bool isIsomorphic(string s, string t) {
137-
vector<int> d1(256);
138-
vector<int> d2(256);
142+
int d1[256]{};
143+
int d2[256]{};
139144
int n = s.size();
140145
for (int i = 0; i < n; ++i) {
141146
char a = s[i], b = t[i];
142-
if (d1[a] != d2[b]) return false;
147+
if (d1[a] != d2[b]) {
148+
return false;
149+
}
143150
d1[a] = d2[b] = i + 1;
144151
}
145152
return true;
@@ -151,13 +158,14 @@ public:
151158
152159
```go
153160
func isIsomorphic(s string, t string) bool {
154-
d1, d2 := make([]int, 256), make([]int, 256)
155-
for i, a := range s {
156-
b := t[i]
157-
if d1[a] != d2[b] {
161+
d1 := [256]int{}
162+
d2 := [256]int{}
163+
for i := range s {
164+
if d1[s[i]] != d2[t[i]] {
158165
return false
159166
}
160-
d1[a], d2[b] = i+1, i+1
167+
d1[s[i]] = i + 1
168+
d2[t[i]] = i + 1
161169
}
162170
return true
163171
}
@@ -168,24 +176,16 @@ func isIsomorphic(s string, t string) bool {
168176
```cs
169177
public class Solution {
170178
public bool IsIsomorphic(string s, string t) {
171-
var d1 = new Dictionary<char, char>();
172-
var d2 = new Dictionary<char, char>();
173-
for (var i = 0; i < s.Length; ++i)
174-
{
175-
char mapping1;
176-
char mapping2;
177-
var found1 = d1.TryGetValue(s[i], out mapping1);
178-
var found2 = d2.TryGetValue(t[i], out mapping2);
179-
if (found1 ^ found2) return false;
180-
if (!found1)
181-
{
182-
d1.Add(s[i], t[i]);
183-
d2.Add(t[i], s[i]);
184-
}
185-
else if (mapping1 != t[i] || mapping2 != s[i])
186-
{
179+
int[] d1 = new int[256];
180+
int[] d2 = new int[256];
181+
for (int i = 0; i < s.Length; ++i) {
182+
var a = s[i];
183+
var b = t[i];
184+
if (d1[a] != d2[b]) {
187185
return false;
188186
}
187+
d1[a] = i + 1;
188+
d2[b] = i + 1;
189189
}
190190
return true;
191191
}
@@ -196,21 +196,18 @@ public class Solution {
196196

197197
```ts
198198
function isIsomorphic(s: string, t: string): boolean {
199-
const n = s.length;
200-
const help = (s: string, t: string) => {
201-
const map = new Map();
202-
for (let i = 0; i < n; i++) {
203-
if (map.has(s[i])) {
204-
if (map.get(s[i]) !== t[i]) {
205-
return false;
206-
}
207-
} else {
208-
map.set(s[i], t[i]);
209-
}
199+
const d1: number[] = new Array(256).fill(0);
200+
const d2: number[] = new Array(256).fill(0);
201+
for (let i = 0; i < s.length; ++i) {
202+
const a = s.charCodeAt(i);
203+
const b = t.charCodeAt(i);
204+
if (d1[a] !== d2[b]) {
205+
return false;
210206
}
211-
return true;
212-
};
213-
return help(s, t) && help(t, s);
207+
d1[a] = i + 1;
208+
d2[b] = i + 1;
209+
}
210+
return true;
214211
}
215212
```
216213

solution/0200-0299/0205.Isomorphic Strings/README_EN.md

+43-44
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,25 @@
3232

3333
## Solutions
3434

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+
3543
<!-- tabs:start -->
3644

3745
### **Python3**
3846

3947
```python
4048
class Solution:
4149
def isIsomorphic(self, s: str, t: str) -> bool:
42-
d1, d2 = {}, {}
50+
d1 = {}
51+
d2 = {}
4352
for a, b in zip(s, t):
44-
if a in d1 and d1[a] != b:
45-
return False
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):
4754
return False
4855
d1[a] = b
4956
d2[b] = a
@@ -54,11 +61,11 @@ class Solution:
5461
class Solution:
5562
def isIsomorphic(self, s: str, t: str) -> bool:
5663
d1, d2 = [0] * 256, [0] * 256
57-
for i, (a, b) in enumerate(zip(s, t)):
64+
for i, (a, b) in enumerate(zip(s, t), 1):
5865
a, b = ord(a), ord(b)
5966
if d1[a] != d2[b]:
6067
return False
61-
d1[a] = d2[b] = i + 1
68+
d1[a] = d2[b] = i
6269
return True
6370
```
6471

@@ -111,12 +118,14 @@ class Solution {
111118
class Solution {
112119
public:
113120
bool isIsomorphic(string s, string t) {
114-
vector<int> d1(256);
115-
vector<int> d2(256);
121+
int d1[256]{};
122+
int d2[256]{};
116123
int n = s.size();
117124
for (int i = 0; i < n; ++i) {
118125
char a = s[i], b = t[i];
119-
if (d1[a] != d2[b]) return false;
126+
if (d1[a] != d2[b]) {
127+
return false;
128+
}
120129
d1[a] = d2[b] = i + 1;
121130
}
122131
return true;
@@ -128,13 +137,14 @@ public:
128137
129138
```go
130139
func isIsomorphic(s string, t string) bool {
131-
d1, d2 := make([]int, 256), make([]int, 256)
132-
for i, a := range s {
133-
b := t[i]
134-
if d1[a] != d2[b] {
140+
d1 := [256]int{}
141+
d2 := [256]int{}
142+
for i := range s {
143+
if d1[s[i]] != d2[t[i]] {
135144
return false
136145
}
137-
d1[a], d2[b] = i+1, i+1
146+
d1[s[i]] = i + 1
147+
d2[t[i]] = i + 1
138148
}
139149
return true
140150
}
@@ -145,24 +155,16 @@ func isIsomorphic(s string, t string) bool {
145155
```cs
146156
public class Solution {
147157
public bool IsIsomorphic(string s, string t) {
148-
var d1 = new Dictionary<char, char>();
149-
var d2 = new Dictionary<char, char>();
150-
for (var i = 0; i < s.Length; ++i)
151-
{
152-
char mapping1;
153-
char mapping2;
154-
var found1 = d1.TryGetValue(s[i], out mapping1);
155-
var found2 = d2.TryGetValue(t[i], out mapping2);
156-
if (found1 ^ found2) return false;
157-
if (!found1)
158-
{
159-
d1.Add(s[i], t[i]);
160-
d2.Add(t[i], s[i]);
161-
}
162-
else if (mapping1 != t[i] || mapping2 != s[i])
163-
{
158+
int[] d1 = new int[256];
159+
int[] d2 = new int[256];
160+
for (int i = 0; i < s.Length; ++i) {
161+
var a = s[i];
162+
var b = t[i];
163+
if (d1[a] != d2[b]) {
164164
return false;
165165
}
166+
d1[a] = i + 1;
167+
d2[b] = i + 1;
166168
}
167169
return true;
168170
}
@@ -173,21 +175,18 @@ public class Solution {
173175

174176
```ts
175177
function isIsomorphic(s: string, t: string): boolean {
176-
const n = s.length;
177-
const help = (s: string, t: string) => {
178-
const map = new Map();
179-
for (let i = 0; i < n; i++) {
180-
if (map.has(s[i])) {
181-
if (map.get(s[i]) !== t[i]) {
182-
return false;
183-
}
184-
} else {
185-
map.set(s[i], t[i]);
186-
}
178+
const d1: number[] = new Array(256).fill(0);
179+
const d2: number[] = new Array(256).fill(0);
180+
for (let i = 0; i < s.length; ++i) {
181+
const a = s.charCodeAt(i);
182+
const b = t.charCodeAt(i);
183+
if (d1[a] !== d2[b]) {
184+
return false;
187185
}
188-
return true;
189-
};
190-
return help(s, t) && help(t, s);
186+
d1[a] = i + 1;
187+
d2[b] = i + 1;
188+
}
189+
return true;
191190
}
192191
```
193192

solution/0200-0299/0205.Isomorphic Strings/Solution.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
class Solution {
22
public:
33
bool isIsomorphic(string s, string t) {
4-
vector<int> d1(256);
5-
vector<int> d2(256);
4+
int d1[256]{};
5+
int d2[256]{};
66
int n = s.size();
77
for (int i = 0; i < n; ++i) {
88
char a = s[i], b = t[i];
9-
if (d1[a] != d2[b]) return false;
9+
if (d1[a] != d2[b]) {
10+
return false;
11+
}
1012
d1[a] = d2[b] = i + 1;
1113
}
1214
return true;

solution/0200-0299/0205.Isomorphic Strings/Solution.cs

+8-16
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
11
public class Solution {
22
public bool IsIsomorphic(string s, string t) {
3-
var d1 = new Dictionary<char, char>();
4-
var d2 = new Dictionary<char, char>();
5-
for (var i = 0; i < s.Length; ++i)
6-
{
7-
char mapping1;
8-
char mapping2;
9-
var found1 = d1.TryGetValue(s[i], out mapping1);
10-
var found2 = d2.TryGetValue(t[i], out mapping2);
11-
if (found1 ^ found2) return false;
12-
if (!found1)
13-
{
14-
d1.Add(s[i], t[i]);
15-
d2.Add(t[i], s[i]);
16-
}
17-
else if (mapping1 != t[i] || mapping2 != s[i])
18-
{
3+
int[] d1 = new int[256];
4+
int[] d2 = new int[256];
5+
for (int i = 0; i < s.Length; ++i) {
6+
var a = s[i];
7+
var b = t[i];
8+
if (d1[a] != d2[b]) {
199
return false;
2010
}
11+
d1[a] = i + 1;
12+
d2[b] = i + 1;
2113
}
2214
return true;
2315
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
func isIsomorphic(s string, t string) bool {
2-
d1, d2 := make([]int, 256), make([]int, 256)
3-
for i, a := range s {
4-
b := t[i]
5-
if d1[a] != d2[b] {
2+
d1 := [256]int{}
3+
d2 := [256]int{}
4+
for i := range s {
5+
if d1[s[i]] != d2[t[i]] {
66
return false
77
}
8-
d1[a], d2[b] = i+1, i+1
8+
d1[s[i]] = i + 1
9+
d2[t[i]] = i + 1
910
}
1011
return true
1112
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
class Solution:
22
def isIsomorphic(self, s: str, t: str) -> bool:
33
d1, d2 = [0] * 256, [0] * 256
4-
for i, (a, b) in enumerate(zip(s, t)):
4+
for i, (a, b) in enumerate(zip(s, t), 1):
55
a, b = ord(a), ord(b)
66
if d1[a] != d2[b]:
77
return False
8-
d1[a] = d2[b] = i + 1
8+
d1[a] = d2[b] = i
99
return True

0 commit comments

Comments
 (0)