Skip to content

Commit 1e2021c

Browse files
committed
feat: add solutions to lc problems: No.0205,0890
* No.0205.Isomorphic Strings * No.0890.Find and Replace Pattern
1 parent 0d2b52e commit 1e2021c

File tree

19 files changed

+493
-77
lines changed

19 files changed

+493
-77
lines changed

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ class Solution:
6565
return True
6666
```
6767

68+
```python
69+
class Solution:
70+
def isIsomorphic(self, s: str, t: str) -> bool:
71+
m1, m2 = [0] * 256, [0] * 256
72+
for i in range(len(s)):
73+
c1, c2 = ord(s[i]), ord(t[i])
74+
if m1[c1] != m2[c2]:
75+
return False
76+
m1[c1] = m2[c2] = i + 1
77+
return True
78+
```
79+
6880
### **Java**
6981

7082
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -86,6 +98,60 @@ class Solution {
8698
}
8799
```
88100

101+
```java
102+
class Solution {
103+
public boolean isIsomorphic(String s, String t) {
104+
int[] m1 = new int[256];
105+
int[] m2 = new int[256];
106+
for (int i = 0; i < s.length(); ++i) {
107+
char c1 = s.charAt(i);
108+
char c2 = t.charAt(i);
109+
if (m1[c1] != m2[c2]) {
110+
return false;
111+
}
112+
m1[c1] = i + 1;
113+
m2[c2] = i + 1;
114+
}
115+
return true;
116+
}
117+
}
118+
```
119+
120+
### **C++**
121+
122+
```cpp
123+
class Solution {
124+
public:
125+
bool isIsomorphic(string s, string t) {
126+
vector<int> m1(256);
127+
vector<int> m2(256);
128+
for (int i = 0; i < s.size(); ++i)
129+
{
130+
if (m1[s[i]] != m2[t[i]]) return 0;
131+
m1[s[i]] = i + 1;
132+
m2[t[i]] = i + 1;
133+
}
134+
return 1;
135+
}
136+
};
137+
```
138+
139+
### **Go**
140+
141+
```go
142+
func isIsomorphic(s string, t string) bool {
143+
m1, m2 := make([]int, 256), make([]int, 256)
144+
for i := 0; i < len(s); i++ {
145+
if m1[s[i]] != m2[t[i]] {
146+
return false
147+
}
148+
m1[s[i]] = i + 1
149+
m2[t[i]] = i + 1
150+
}
151+
return true
152+
}
153+
```
154+
89155
### **...**
90156

91157
```

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ class Solution:
5050
return True
5151
```
5252

53+
```python
54+
class Solution:
55+
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]:
60+
return False
61+
m1[c1] = m2[c2] = i + 1
62+
return True
63+
```
64+
5365
### **Java**
5466

5567
```java
@@ -69,6 +81,60 @@ class Solution {
6981
}
7082
```
7183

84+
```java
85+
class Solution {
86+
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]) {
93+
return false;
94+
}
95+
m1[c1] = i + 1;
96+
m2[c2] = i + 1;
97+
}
98+
return true;
99+
}
100+
}
101+
```
102+
103+
### **C++**
104+
105+
```cpp
106+
class Solution {
107+
public:
108+
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)
112+
{
113+
if (m1[s[i]] != m2[t[i]]) return 0;
114+
m1[s[i]] = i + 1;
115+
m2[t[i]] = i + 1;
116+
}
117+
return 1;
118+
}
119+
};
120+
```
121+
122+
### **Go**
123+
124+
```go
125+
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]] {
129+
return false
130+
}
131+
m1[s[i]] = i + 1
132+
m2[t[i]] = i + 1
133+
}
134+
return true
135+
}
136+
```
137+
72138
### **...**
73139

74140
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
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)
7+
{
8+
if (m1[s[i]] != m2[t[i]]) return 0;
9+
m1[s[i]] = i + 1;
10+
m2[t[i]] = i + 1;
11+
}
12+
return 1;
13+
}
14+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func isIsomorphic(s string, t string) bool {
2+
m1, m2 := make([]int, 256), make([]int, 256)
3+
for i := 0; i < len(s); i++ {
4+
if m1[s[i]] != m2[t[i]] {
5+
return false
6+
}
7+
m1[s[i]] = i + 1
8+
m2[t[i]] = i + 1
9+
}
10+
return true
11+
}

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
class Solution {
22
public boolean isIsomorphic(String s, String t) {
3-
int n = s.length();
4-
Map<Character, Character> a2b = new HashMap<>();
5-
Map<Character, Character> b2a = new HashMap<>();
6-
for (int i = 0; i < n; ++i) {
7-
char a = s.charAt(i), b = t.charAt(i);
8-
if ((a2b.containsKey(a) && a2b.get(a) != b) || (b2a.containsKey(b) && b2a.get(b) != a)) return false;
9-
a2b.put(a, b);
10-
b2a.put(b, a);
3+
int[] m1 = new int[256];
4+
int[] m2 = new int[256];
5+
for (int i = 0; i < s.length(); ++i) {
6+
char c1 = s.charAt(i);
7+
char c2 = t.charAt(i);
8+
if (m1[c1] != m2[c2]) {
9+
return false;
10+
}
11+
m1[c1] = i + 1;
12+
m2[c2] = i + 1;
1113
}
1214
return true;
1315
}
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
class Solution:
22
def isIsomorphic(self, s: str, t: str) -> bool:
3-
a2b, b2a = {}, {}
4-
n = len(s)
5-
for i in range(n):
6-
a, b = s[i], t[i]
7-
if (a in a2b and a2b[a] != b) or (b in b2a and b2a[b] != a):
3+
m1, m2 = [0] * 256, [0] * 256
4+
for i in range(len(s)):
5+
c1, c2 = ord(s[i]), ord(t[i])
6+
if m1[c1] != m2[c2]:
87
return False
9-
a2b[a] = b
10-
b2a[b] = a
8+
m1[c1] = m2[c2] = i + 1
119
return True

solution/0700-0799/0763.Partition Labels/README.md

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@
3434

3535
<!-- 这里可写通用的实现逻辑 -->
3636

37+
先用数组或哈希表 last 记录每个字母最后一次出现的位置。
38+
39+
接下来使用贪心的方法,将字符串划分为尽可能多的片段:
40+
41+
- 从左到右遍历字符串,遍历的同时维护当前片段的开始下标 left 和结束下标 right,初始均为 0;
42+
- 对于每个访问到的字母 c,获取到最后一次出现的位置 `last[c]`。由于当前片段的结束下标一定不会小于 `last[c]`,因此令 `right = max(right, last[c])`
43+
- 当访问到下标 right 时,当前片段访问结束,当前片段的下标范围是 `[left, right]`,长度为 `right - left + 1`,将其添加到结果数组中,然后令 left = right + 1, 继续寻找下一个片段;
44+
- 重复上述过程,直至字符串遍历结束。
45+
3746
<!-- tabs:start -->
3847

3948
### **Python3**
@@ -43,14 +52,13 @@
4352
```python
4453
class Solution:
4554
def partitionLabels(self, s: str) -> List[int]:
46-
last = defaultdict(int)
47-
n = len(s)
48-
for i in range(n):
49-
last[s[i]] = i
55+
last = [0] * 26
56+
for i, c in enumerate(s):
57+
last[ord(c) - ord('a')] = i
5058
ans = []
5159
left = right = 0
52-
for i in range(n):
53-
right = max(right, last[s[i]])
60+
for i, c in enumerate(s):
61+
right = max(right, last[ord(c) - ord('a')])
5462
if i == right:
5563
ans.append(right - left + 1)
5664
left = right + 1
@@ -64,14 +72,14 @@ class Solution:
6472
```java
6573
class Solution {
6674
public List<Integer> partitionLabels(String s) {
67-
int[] last = new int[128];
75+
int[] last = new int[26];
6876
int n = s.length();
6977
for (int i = 0; i < n; ++i) {
70-
last[s.charAt(i)] = i;
78+
last[s.charAt(i) - 'a'] = i;
7179
}
7280
List<Integer> ans = new ArrayList<>();
7381
for (int i = 0, left = 0, right = 0; i < n; ++i) {
74-
right = Math.max(right, last[s.charAt(i)]);
82+
right = Math.max(right, last[s.charAt(i) - 'a']);
7583
if (i == right) {
7684
ans.add(right - left + 1);
7785
left = right + 1;
@@ -87,15 +95,15 @@ class Solution {
8795
```ts
8896
function partitionLabels(s: string): number[] {
8997
const n = s.length;
90-
let last = new Array(128);
98+
let last = new Array(26);
9199
for (let i = 0; i < n; i++) {
92-
last[s.charCodeAt(i)] = i;
100+
last[s.charCodeAt(i) - 'a'.charCodeAt(0)] = i;
93101
}
94102
let ans = [];
95103
let left = 0,
96104
right = 0;
97105
for (let i = 0; i < n; i++) {
98-
right = Math.max(right, last[s.charCodeAt(i)]);
106+
right = Math.max(right, last[s.charCodeAt(i) - 'a'.charCodeAt(0)]);
99107
if (i == right) {
100108
ans.push(right - left + 1);
101109
left = right + 1;
@@ -111,13 +119,13 @@ function partitionLabels(s: string): number[] {
111119
class Solution {
112120
public:
113121
vector<int> partitionLabels(string s) {
122+
vector<int> last(26);
114123
int n = s.size();
115-
vector<int> last(128);
116-
for (int i = 0; i < n; ++i) last[s[i]] = i;
124+
for (int i = 0; i < n; ++i) last[s[i] - 'a'] = i;
117125
vector<int> ans;
118126
for (int i = 0, left = 0, right = 0; i < n; ++i)
119127
{
120-
right = max(right, last[s[i]]);
128+
right = max(right, last[s[i] - 'a']);
121129
if (i == right)
122130
{
123131
ans.push_back(right - left + 1);
@@ -133,15 +141,14 @@ public:
133141
134142
```go
135143
func partitionLabels(s string) []int {
144+
last := make([]int, 26)
136145
n := len(s)
137-
last := make([]int, 128)
138146
for i := 0; i < n; i++ {
139-
last[s[i]] = i
147+
last[s[i]-'a'] = i
140148
}
141149
var ans []int
142-
left, right := 0, 0
143-
for i := 0; i < n; i++ {
144-
right = max(right, last[s[i]])
150+
for i, left, right := 0, 0, 0; i < n; i++ {
151+
right = max(right, last[s[i]-'a'])
145152
if i == right {
146153
ans = append(ans, right-left+1)
147154
left = right + 1

0 commit comments

Comments
 (0)