Skip to content

Commit 13d4f5a

Browse files
committed
feat: add solutions to lc problem: No.1156
No.1156.Swap For Longest Repeated Character Substring
1 parent 6377837 commit 13d4f5a

File tree

6 files changed

+335
-2
lines changed

6 files changed

+335
-2
lines changed

solution/1100-1199/1156.Swap For Longest Repeated Character Substring/README.md

+120-1
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,141 @@
5555

5656
<!-- 这里可写通用的实现逻辑 -->
5757

58+
**方法一:双指针**
59+
60+
我们先统计每个字符出现的次数,记录在数组 `cnt` 中。
61+
62+
然后我们使用双指针 $i$ 和 $j$,初始时 $i = j = 0$,然后我们不断地向右移动 $j$,直到 $j$ 指向的字符与 $i$ 指向的字符不同,此时我们得到了一个长度为 $l = j - i$ 的子串,其中所有字符都相同。然后我们跳过第 $j$ 个字符,用指针 $k$ 继续向右移动,直到 $k$ 指向的字符与 $i$ 指向的字符不同,此时我们得到了一个长度为 $r = k - j - 1$ 的子串,其中所有字符都相同。此时我们可以得到的最长子串长度为 $min(l + r + 1, cnt[text[i]])$,其中 $cnt[text[i]]$ 表示字符 $text[i]$ 出现的次数。我们将这个值与当前的最大值进行比较,取较大值作为答案。
63+
64+
时间复杂度为 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 为字符串的长度,而 $C$ 为字符集的大小。本题中 $C = 26$。
65+
5866
<!-- tabs:start -->
5967

6068
### **Python3**
6169

6270
<!-- 这里可写当前语言的特殊实现逻辑 -->
6371

6472
```python
65-
73+
class Solution:
74+
def maxRepOpt1(self, text: str) -> int:
75+
cnt = Counter(text)
76+
n = len(text)
77+
ans = i = 0
78+
while i < n:
79+
j = i
80+
while j < n and text[j] == text[i]:
81+
j += 1
82+
l = j - i
83+
k = j + 1
84+
while k < n and text[k] == text[i]:
85+
k += 1
86+
r = k - j - 1
87+
ans = max(ans, min(l + r + 1, cnt[text[i]]))
88+
i = j
89+
return ans
6690
```
6791

6892
### **Java**
6993

7094
<!-- 这里可写当前语言的特殊实现逻辑 -->
7195

7296
```java
97+
class Solution {
98+
public int maxRepOpt1(String text) {
99+
int[] cnt = new int[26];
100+
int n = text.length();
101+
for (int i = 0; i < n; ++i) {
102+
++cnt[text.charAt(i) - 'a'];
103+
}
104+
int ans = 0, i = 0;
105+
while (i < n) {
106+
int j = i;
107+
while (j < n && text.charAt(j) == text.charAt(i)) {
108+
++j;
109+
}
110+
int l = j - i;
111+
int k = j + 1;
112+
while (k < n && text.charAt(k) == text.charAt(i)) {
113+
++k;
114+
}
115+
int r = k - j - 1;
116+
ans = Math.max(ans, Math.min(l + r + 1, cnt[text.charAt(i) - 'a']));
117+
i = j;
118+
}
119+
return ans;
120+
}
121+
}
122+
```
123+
124+
### **C++**
125+
126+
```cpp
127+
class Solution {
128+
public:
129+
int maxRepOpt1(string text) {
130+
int cnt[26] = {0};
131+
for (char& c : text) {
132+
++cnt[c - 'a'];
133+
}
134+
int n = text.size();
135+
int ans = 0, i = 0;
136+
while (i < n) {
137+
int j = i;
138+
while (j < n && text[j] == text[i]) {
139+
++j;
140+
}
141+
int l = j - i;
142+
int k = j + 1;
143+
while (k < n && text[k] == text[i]) {
144+
++k;
145+
}
146+
int r = k - j - 1;
147+
ans = max(ans, min(l + r + 1, cnt[text[i] - 'a']));
148+
i = j;
149+
}
150+
return ans;
151+
}
152+
};
153+
```
73154
155+
### **Go**
156+
157+
```go
158+
func maxRepOpt1(text string) (ans int) {
159+
cnt := [26]int{}
160+
for _, c := range text {
161+
cnt[c-'a']++
162+
}
163+
n := len(text)
164+
for i, j := 0, 0; i < n; i = j {
165+
j = i
166+
for j < n && text[j] == text[i] {
167+
j++
168+
}
169+
l := j - i
170+
k := j + 1
171+
for k < n && text[k] == text[i] {
172+
k++
173+
}
174+
r := k - j - 1
175+
ans = max(ans, min(l+r+1, cnt[text[i]-'a']))
176+
}
177+
return
178+
}
179+
180+
func max(a, b int) int {
181+
if a > b {
182+
return a
183+
}
184+
return b
185+
}
186+
187+
func min(a, b int) int {
188+
if a < b {
189+
return a
190+
}
191+
return b
192+
}
74193
```
75194

76195
### **...**

solution/1100-1199/1156.Swap For Longest Repeated Character Substring/README_EN.md

+112-1
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,124 @@
4848
### **Python3**
4949

5050
```python
51-
51+
class Solution:
52+
def maxRepOpt1(self, text: str) -> int:
53+
cnt = Counter(text)
54+
n = len(text)
55+
ans = i = 0
56+
while i < n:
57+
j = i
58+
while j < n and text[j] == text[i]:
59+
j += 1
60+
l = j - i
61+
k = j + 1
62+
while k < n and text[k] == text[i]:
63+
k += 1
64+
r = k - j - 1
65+
ans = max(ans, min(l + r + 1, cnt[text[i]]))
66+
i = j
67+
return ans
5268
```
5369

5470
### **Java**
5571

5672
```java
73+
class Solution {
74+
public int maxRepOpt1(String text) {
75+
int[] cnt = new int[26];
76+
int n = text.length();
77+
for (int i = 0; i < n; ++i) {
78+
++cnt[text.charAt(i) - 'a'];
79+
}
80+
int ans = 0, i = 0;
81+
while (i < n) {
82+
int j = i;
83+
while (j < n && text.charAt(j) == text.charAt(i)) {
84+
++j;
85+
}
86+
int l = j - i;
87+
int k = j + 1;
88+
while (k < n && text.charAt(k) == text.charAt(i)) {
89+
++k;
90+
}
91+
int r = k - j - 1;
92+
ans = Math.max(ans, Math.min(l + r + 1, cnt[text.charAt(i) - 'a']));
93+
i = j;
94+
}
95+
return ans;
96+
}
97+
}
98+
```
99+
100+
### **C++**
101+
102+
```cpp
103+
class Solution {
104+
public:
105+
int maxRepOpt1(string text) {
106+
int cnt[26] = {0};
107+
for (char& c : text) {
108+
++cnt[c - 'a'];
109+
}
110+
int n = text.size();
111+
int ans = 0, i = 0;
112+
while (i < n) {
113+
int j = i;
114+
while (j < n && text[j] == text[i]) {
115+
++j;
116+
}
117+
int l = j - i;
118+
int k = j + 1;
119+
while (k < n && text[k] == text[i]) {
120+
++k;
121+
}
122+
int r = k - j - 1;
123+
ans = max(ans, min(l + r + 1, cnt[text[i] - 'a']));
124+
i = j;
125+
}
126+
return ans;
127+
}
128+
};
129+
```
57130
131+
### **Go**
132+
133+
```go
134+
func maxRepOpt1(text string) (ans int) {
135+
cnt := [26]int{}
136+
for _, c := range text {
137+
cnt[c-'a']++
138+
}
139+
n := len(text)
140+
for i, j := 0, 0; i < n; i = j {
141+
j = i
142+
for j < n && text[j] == text[i] {
143+
j++
144+
}
145+
l := j - i
146+
k := j + 1
147+
for k < n && text[k] == text[i] {
148+
k++
149+
}
150+
r := k - j - 1
151+
ans = max(ans, min(l+r+1, cnt[text[i]-'a']))
152+
}
153+
return
154+
}
155+
156+
func max(a, b int) int {
157+
if a > b {
158+
return a
159+
}
160+
return b
161+
}
162+
163+
func min(a, b int) int {
164+
if a < b {
165+
return a
166+
}
167+
return b
168+
}
58169
```
59170

60171
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
int maxRepOpt1(string text) {
4+
int cnt[26] = {0};
5+
for (char& c : text) {
6+
++cnt[c - 'a'];
7+
}
8+
int n = text.size();
9+
int ans = 0, i = 0;
10+
while (i < n) {
11+
int j = i;
12+
while (j < n && text[j] == text[i]) {
13+
++j;
14+
}
15+
int l = j - i;
16+
int k = j + 1;
17+
while (k < n && text[k] == text[i]) {
18+
++k;
19+
}
20+
int r = k - j - 1;
21+
ans = max(ans, min(l + r + 1, cnt[text[i] - 'a']));
22+
i = j;
23+
}
24+
return ans;
25+
}
26+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
func maxRepOpt1(text string) (ans int) {
2+
cnt := [26]int{}
3+
for _, c := range text {
4+
cnt[c-'a']++
5+
}
6+
n := len(text)
7+
for i, j := 0, 0; i < n; i = j {
8+
j = i
9+
for j < n && text[j] == text[i] {
10+
j++
11+
}
12+
l := j - i
13+
k := j + 1
14+
for k < n && text[k] == text[i] {
15+
k++
16+
}
17+
r := k - j - 1
18+
ans = max(ans, min(l+r+1, cnt[text[i]-'a']))
19+
}
20+
return
21+
}
22+
23+
func max(a, b int) int {
24+
if a > b {
25+
return a
26+
}
27+
return b
28+
}
29+
30+
func min(a, b int) int {
31+
if a < b {
32+
return a
33+
}
34+
return b
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public int maxRepOpt1(String text) {
3+
int[] cnt = new int[26];
4+
int n = text.length();
5+
for (int i = 0; i < n; ++i) {
6+
++cnt[text.charAt(i) - 'a'];
7+
}
8+
int ans = 0, i = 0;
9+
while (i < n) {
10+
int j = i;
11+
while (j < n && text.charAt(j) == text.charAt(i)) {
12+
++j;
13+
}
14+
int l = j - i;
15+
int k = j + 1;
16+
while (k < n && text.charAt(k) == text.charAt(i)) {
17+
++k;
18+
}
19+
int r = k - j - 1;
20+
ans = Math.max(ans, Math.min(l + r + 1, cnt[text.charAt(i) - 'a']));
21+
i = j;
22+
}
23+
return ans;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def maxRepOpt1(self, text: str) -> int:
3+
cnt = Counter(text)
4+
n = len(text)
5+
ans = i = 0
6+
while i < n:
7+
j = i
8+
while j < n and text[j] == text[i]:
9+
j += 1
10+
l = j - i
11+
k = j + 1
12+
while k < n and text[k] == text[i]:
13+
k += 1
14+
r = k - j - 1
15+
ans = max(ans, min(l + r + 1, cnt[text[i]]))
16+
i = j
17+
return ans

0 commit comments

Comments
 (0)