Skip to content

Commit c7cae77

Browse files
committed
feat: update solutions to lc problems: No.0205,0392
* No.0205.Isomorphic Strings * No.0392.Is Subsequence
1 parent 3092c40 commit c7cae77

File tree

11 files changed

+139
-134
lines changed

11 files changed

+139
-134
lines changed

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

+51-46
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949

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

52+
**方法一:哈希表**
53+
5254
<!-- tabs:start -->
5355

5456
### **Python3**
@@ -58,26 +60,26 @@
5860
```python
5961
class Solution:
6062
def isIsomorphic(self, s: str, t: str) -> bool:
61-
a2b, b2a = {}, {}
62-
n = len(s)
63-
for i in range(n):
64-
a, b = s[i], t[i]
65-
if (a in a2b and a2b[a] != b) or (b in b2a and b2a[b] != a):
63+
d1, d2 = {}, {}
64+
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:
6668
return False
67-
a2b[a] = b
68-
b2a[b] = a
69+
d1[a] = b
70+
d2[b] = a
6971
return True
7072
```
7173

7274
```python
7375
class Solution:
7476
def isIsomorphic(self, s: str, t: str) -> bool:
75-
m1, m2 = [0] * 256, [0] * 256
76-
for i in range(len(s)):
77-
c1, c2 = ord(s[i]), ord(t[i])
78-
if m1[c1] != m2[c2]:
77+
d1, d2 = [0] * 256, [0] * 256
78+
for i, (a, b) in enumerate(zip(s, t)):
79+
a, b = ord(a), ord(b)
80+
if d1[a] != d2[b]:
7981
return False
80-
m1[c1] = m2[c2] = i + 1
82+
d1[a] = d2[b] = i + 1
8183
return True
8284
```
8385

@@ -88,14 +90,19 @@ class Solution:
8890
```java
8991
class Solution {
9092
public boolean isIsomorphic(String s, String t) {
93+
Map<Character, Character> d1 = new HashMap<>();
94+
Map<Character, Character> d2 = new HashMap<>();
9195
int n = s.length();
92-
Map<Character, Character> a2b = new HashMap<>();
93-
Map<Character, Character> b2a = new HashMap<>();
9496
for (int i = 0; i < n; ++i) {
9597
char a = s.charAt(i), b = t.charAt(i);
96-
if ((a2b.containsKey(a) && a2b.get(a) != b) || (b2a.containsKey(b) && b2a.get(b) != a)) return false;
97-
a2b.put(a, b);
98-
b2a.put(b, a);
98+
if (d1.containsKey(a) && d1.get(a) != b) {
99+
return false;
100+
}
101+
if (d2.containsKey(b) && d2.get(b) != a) {
102+
return false;
103+
}
104+
d1.put(a, b);
105+
d2.put(b, a);
99106
}
100107
return true;
101108
}
@@ -105,16 +112,16 @@ class Solution {
105112
```java
106113
class Solution {
107114
public boolean isIsomorphic(String s, String t) {
108-
int[] m1 = new int[256];
109-
int[] m2 = new int[256];
110-
for (int i = 0; i < s.length(); ++i) {
111-
char c1 = s.charAt(i);
112-
char c2 = t.charAt(i);
113-
if (m1[c1] != m2[c2]) {
115+
int[] d1 = new int[256];
116+
int[] d2 = new int[256];
117+
int n = s.length();
118+
for (int i = 0; i < n; ++i) {
119+
char a = s.charAt(i), b = t.charAt(i);
120+
if (d1[a] != d2[b]) {
114121
return false;
115122
}
116-
m1[c1] = i + 1;
117-
m2[c2] = i + 1;
123+
d1[a] = i + 1;
124+
d2[b] = i + 1;
118125
}
119126
return true;
120127
}
@@ -127,15 +134,16 @@ class Solution {
127134
class Solution {
128135
public:
129136
bool isIsomorphic(string s, string t) {
130-
vector<int> m1(256);
131-
vector<int> m2(256);
132-
for (int i = 0; i < s.size(); ++i)
137+
vector<int> d1(256);
138+
vector<int> d2(256);
139+
int n = s.size();
140+
for (int i = 0; i < n; ++i)
133141
{
134-
if (m1[s[i]] != m2[t[i]]) return 0;
135-
m1[s[i]] = i + 1;
136-
m2[t[i]] = i + 1;
142+
char a = s[i], b = t[i];
143+
if (d1[a] != d2[b]) return false;
144+
d1[a] = d2[b] = i + 1;
137145
}
138-
return 1;
146+
return true;
139147
}
140148
};
141149
```
@@ -144,13 +152,13 @@ public:
144152
145153
```go
146154
func isIsomorphic(s string, t string) bool {
147-
m1, m2 := make([]int, 256), make([]int, 256)
148-
for i := 0; i < len(s); i++ {
149-
if m1[s[i]] != m2[t[i]] {
155+
d1, d2 := make([]int, 256), make([]int, 256)
156+
for i, a := range s {
157+
b := t[i]
158+
if d1[a] != d2[b] {
150159
return false
151160
}
152-
m1[s[i]] = i + 1
153-
m2[t[i]] = i + 1
161+
d1[a], d2[b] = i+1, i+1
154162
}
155163
return true
156164
}
@@ -159,24 +167,21 @@ func isIsomorphic(s string, t string) bool {
159167
### **C#**
160168

161169
```cs
162-
using System.Collections.Generic;
163-
164170
public class Solution {
165171
public bool IsIsomorphic(string s, string t) {
166-
if (s.Length != t.Length) return false;
167-
var dict1 = new Dictionary<char, char>();
168-
var dict2 = new Dictionary<char, char>();
172+
var d1 = new Dictionary<char, char>();
173+
var d2 = new Dictionary<char, char>();
169174
for (var i = 0; i < s.Length; ++i)
170175
{
171176
char mapping1;
172177
char mapping2;
173-
var found1 = dict1.TryGetValue(s[i], out mapping1);
174-
var found2 = dict2.TryGetValue(t[i], out mapping2);
178+
var found1 = d1.TryGetValue(s[i], out mapping1);
179+
var found2 = d2.TryGetValue(t[i], out mapping2);
175180
if (found1 ^ found2) return false;
176181
if (!found1)
177182
{
178-
dict1.Add(s[i], t[i]);
179-
dict2.Add(t[i], s[i]);
183+
d1.Add(s[i], t[i]);
184+
d2.Add(t[i], s[i]);
180185
}
181186
else if (mapping1 != t[i] || mapping2 != s[i])
182187
{

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

+49-46
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,26 @@
3939
```python
4040
class Solution:
4141
def isIsomorphic(self, s: str, t: str) -> bool:
42-
a2b, b2a = {}, {}
43-
n = len(s)
44-
for i in range(n):
45-
a, b = s[i], t[i]
46-
if (a in a2b and a2b[a] != b) or (b in b2a and b2a[b] != a):
42+
d1, d2 = {}, {}
43+
for a, b in zip(s, t):
44+
if a in d1 and d1[a] != b:
4745
return False
48-
a2b[a] = b
49-
b2a[b] = a
46+
if b in d2 and d2[b] != a:
47+
return False
48+
d1[a] = b
49+
d2[b] = a
5050
return True
5151
```
5252

5353
```python
5454
class Solution:
5555
def isIsomorphic(self, s: str, t: str) -> bool:
56-
m1, m2 = [0] * 256, [0] * 256
57-
for i in range(len(s)):
58-
c1, c2 = ord(s[i]), ord(t[i])
59-
if m1[c1] != m2[c2]:
56+
d1, d2 = [0] * 256, [0] * 256
57+
for i, (a, b) in enumerate(zip(s, t)):
58+
a, b = ord(a), ord(b)
59+
if d1[a] != d2[b]:
6060
return False
61-
m1[c1] = m2[c2] = i + 1
61+
d1[a] = d2[b] = i + 1
6262
return True
6363
```
6464

@@ -67,14 +67,19 @@ class Solution:
6767
```java
6868
class Solution {
6969
public boolean isIsomorphic(String s, String t) {
70+
Map<Character, Character> d1 = new HashMap<>();
71+
Map<Character, Character> d2 = new HashMap<>();
7072
int n = s.length();
71-
Map<Character, Character> a2b = new HashMap<>();
72-
Map<Character, Character> b2a = new HashMap<>();
7373
for (int i = 0; i < n; ++i) {
7474
char a = s.charAt(i), b = t.charAt(i);
75-
if ((a2b.containsKey(a) && a2b.get(a) != b) || (b2a.containsKey(b) && b2a.get(b) != a)) return false;
76-
a2b.put(a, b);
77-
b2a.put(b, a);
75+
if (d1.containsKey(a) && d1.get(a) != b) {
76+
return false;
77+
}
78+
if (d2.containsKey(b) && d2.get(b) != a) {
79+
return false;
80+
}
81+
d1.put(a, b);
82+
d2.put(b, a);
7883
}
7984
return true;
8085
}
@@ -84,16 +89,16 @@ class Solution {
8489
```java
8590
class Solution {
8691
public boolean isIsomorphic(String s, String t) {
87-
int[] m1 = new int[256];
88-
int[] m2 = new int[256];
89-
for (int i = 0; i < s.length(); ++i) {
90-
char c1 = s.charAt(i);
91-
char c2 = t.charAt(i);
92-
if (m1[c1] != m2[c2]) {
92+
int[] d1 = new int[256];
93+
int[] d2 = new int[256];
94+
int n = s.length();
95+
for (int i = 0; i < n; ++i) {
96+
char a = s.charAt(i), b = t.charAt(i);
97+
if (d1[a] != d2[b]) {
9398
return false;
9499
}
95-
m1[c1] = i + 1;
96-
m2[c2] = i + 1;
100+
d1[a] = i + 1;
101+
d2[b] = i + 1;
97102
}
98103
return true;
99104
}
@@ -106,15 +111,16 @@ class Solution {
106111
class Solution {
107112
public:
108113
bool isIsomorphic(string s, string t) {
109-
vector<int> m1(256);
110-
vector<int> m2(256);
111-
for (int i = 0; i < s.size(); ++i)
114+
vector<int> d1(256);
115+
vector<int> d2(256);
116+
int n = s.size();
117+
for (int i = 0; i < n; ++i)
112118
{
113-
if (m1[s[i]] != m2[t[i]]) return 0;
114-
m1[s[i]] = i + 1;
115-
m2[t[i]] = i + 1;
119+
char a = s[i], b = t[i];
120+
if (d1[a] != d2[b]) return false;
121+
d1[a] = d2[b] = i + 1;
116122
}
117-
return 1;
123+
return true;
118124
}
119125
};
120126
```
@@ -123,13 +129,13 @@ public:
123129
124130
```go
125131
func isIsomorphic(s string, t string) bool {
126-
m1, m2 := make([]int, 256), make([]int, 256)
127-
for i := 0; i < len(s); i++ {
128-
if m1[s[i]] != m2[t[i]] {
132+
d1, d2 := make([]int, 256), make([]int, 256)
133+
for i, a := range s {
134+
b := t[i]
135+
if d1[a] != d2[b] {
129136
return false
130137
}
131-
m1[s[i]] = i + 1
132-
m2[t[i]] = i + 1
138+
d1[a], d2[b] = i+1, i+1
133139
}
134140
return true
135141
}
@@ -138,24 +144,21 @@ func isIsomorphic(s string, t string) bool {
138144
### **C#**
139145

140146
```cs
141-
using System.Collections.Generic;
142-
143147
public class Solution {
144148
public bool IsIsomorphic(string s, string t) {
145-
if (s.Length != t.Length) return false;
146-
var dict1 = new Dictionary<char, char>();
147-
var dict2 = new Dictionary<char, char>();
149+
var d1 = new Dictionary<char, char>();
150+
var d2 = new Dictionary<char, char>();
148151
for (var i = 0; i < s.Length; ++i)
149152
{
150153
char mapping1;
151154
char mapping2;
152-
var found1 = dict1.TryGetValue(s[i], out mapping1);
153-
var found2 = dict2.TryGetValue(t[i], out mapping2);
155+
var found1 = d1.TryGetValue(s[i], out mapping1);
156+
var found2 = d2.TryGetValue(t[i], out mapping2);
154157
if (found1 ^ found2) return false;
155158
if (!found1)
156159
{
157-
dict1.Add(s[i], t[i]);
158-
dict2.Add(t[i], s[i]);
160+
d1.Add(s[i], t[i]);
161+
d2.Add(t[i], s[i]);
159162
}
160163
else if (mapping1 != t[i] || mapping2 != s[i])
161164
{
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
class Solution {
22
public:
33
bool isIsomorphic(string s, string t) {
4-
vector<int> m1(256);
5-
vector<int> m2(256);
6-
for (int i = 0; i < s.size(); ++i)
4+
vector<int> d1(256);
5+
vector<int> d2(256);
6+
int n = s.size();
7+
for (int i = 0; i < n; ++i)
78
{
8-
if (m1[s[i]] != m2[t[i]]) return 0;
9-
m1[s[i]] = i + 1;
10-
m2[t[i]] = i + 1;
9+
char a = s[i], b = t[i];
10+
if (d1[a] != d2[b]) return false;
11+
d1[a] = d2[b] = i + 1;
1112
}
12-
return 1;
13+
return true;
1314
}
1415
};

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

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
using System.Collections.Generic;
2-
31
public class Solution {
42
public bool IsIsomorphic(string s, string t) {
5-
if (s.Length != t.Length) return false;
6-
var dict1 = new Dictionary<char, char>();
7-
var dict2 = new Dictionary<char, char>();
3+
var d1 = new Dictionary<char, char>();
4+
var d2 = new Dictionary<char, char>();
85
for (var i = 0; i < s.Length; ++i)
96
{
107
char mapping1;
118
char mapping2;
12-
var found1 = dict1.TryGetValue(s[i], out mapping1);
13-
var found2 = dict2.TryGetValue(t[i], out mapping2);
9+
var found1 = d1.TryGetValue(s[i], out mapping1);
10+
var found2 = d2.TryGetValue(t[i], out mapping2);
1411
if (found1 ^ found2) return false;
1512
if (!found1)
1613
{
17-
dict1.Add(s[i], t[i]);
18-
dict2.Add(t[i], s[i]);
14+
d1.Add(s[i], t[i]);
15+
d2.Add(t[i], s[i]);
1916
}
2017
else if (mapping1 != t[i] || mapping2 != s[i])
2118
{

0 commit comments

Comments
 (0)