Skip to content

Commit 8a61401

Browse files
committed
feat: update solutions to lc problem: No.1576
No.1576.Replace All 's to Avoid Consecutive Repeating Characters
1 parent 7828ead commit 8a61401

File tree

7 files changed

+152
-144
lines changed

7 files changed

+152
-144
lines changed

solution/1500-1599/1576.Replace All 's to Avoid Consecutive Repeating Characters/README.md

Lines changed: 58 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@
4848

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

51+
**方法一:模拟**
52+
53+
我们遍历字符串,对于每个位置,如果该位置是 `?`,则枚举字符 `'a'``'b'``'c'`,如果该字符 $c$ 与前后字符都不相同,则将该位置替换为该字符,否则继续枚举下一个字符。
54+
55+
遍历结束后,返回字符串即可。
56+
57+
时间复杂度 $O(n)$,其中 $n$ 为字符串的长度。忽略答案的空间消耗,空间复杂度 $O(1)$。
58+
5159
<!-- tabs:start -->
5260

5361
### **Python3**
@@ -57,17 +65,16 @@
5765
```python
5866
class Solution:
5967
def modifyString(self, s: str) -> str:
60-
ans = list(s)
61-
for i, c in enumerate(ans):
62-
if c == '?':
63-
for cc in 'abc':
64-
if i > 0 and ans[i - 1] == cc:
65-
continue
66-
if i < len(s) - 1 and ans[i + 1] == cc:
68+
s = list(s)
69+
n = len(s)
70+
for i in range(n):
71+
if s[i] == "?":
72+
for c in "abc":
73+
if (i and s[i - 1] == c) or (i + 1 < n and s[i + 1] == c):
6774
continue
68-
ans[i] = cc
75+
s[i] = c
6976
break
70-
return ''.join(ans)
77+
return "".join(s)
7178
```
7279

7380
### **Java**
@@ -77,23 +84,20 @@ class Solution:
7784
```java
7885
class Solution {
7986
public String modifyString(String s) {
80-
char[] chars = s.toCharArray();
81-
for (int i = 0; i < chars.length; ++i) {
82-
char c = chars[i];
83-
if (c == '?') {
84-
for (char cc = 'a'; cc <= 'c'; ++cc) {
85-
if (i > 0 && chars[i - 1] == cc) {
87+
char[] cs = s.toCharArray();
88+
int n = cs.length;
89+
for (int i = 0; i < n; ++i) {
90+
if (cs[i] == '?') {
91+
for (char c = 'a'; c <= 'c'; ++c) {
92+
if ((i > 0 && cs[i - 1] == c) || (i + 1 < n && cs[i + 1] == c)) {
8693
continue;
8794
}
88-
if (i < chars.length - 1 && chars[i + 1] == cc) {
89-
continue;
90-
}
91-
chars[i] = cc;
95+
cs[i] = c;
9296
break;
9397
}
9498
}
9599
}
96-
return String.valueOf(chars);
100+
return String.valueOf(cs);
97101
}
98102
}
99103
```
@@ -104,12 +108,14 @@ class Solution {
104108
class Solution {
105109
public:
106110
string modifyString(string s) {
107-
for (int i = 0; i < s.size(); ++i) {
111+
int n = s.size();
112+
for (int i = 0; i < n; ++i) {
108113
if (s[i] == '?') {
109-
for (char cc : "abc") {
110-
if (i > 0 && s[i - 1] == cc) continue;
111-
if (i < s.size() - 1 && s[i + 1] == cc) continue;
112-
s[i] = cc;
114+
for (char c : "abc") {
115+
if ((i && s[i - 1] == c) || (i + 1 < n && s[i + 1] == c)) {
116+
continue;
117+
}
118+
s[i] = c;
113119
break;
114120
}
115121
}
@@ -123,47 +129,51 @@ public:
123129
124130
```go
125131
func modifyString(s string) string {
126-
ans := []byte(s)
127-
for i, c := range ans {
128-
if c == '?' {
129-
for cc := byte('a'); cc <= 'c'; cc++ {
130-
if i > 0 && ans[i-1] == cc {
132+
n := len(s)
133+
cs := []byte(s)
134+
for i := range s {
135+
if cs[i] == '?' {
136+
for c := byte('a'); c <= byte('c'); c++ {
137+
if (i > 0 && cs[i-1] == c) || (i+1 < n && cs[i+1] == c) {
131138
continue
132139
}
133-
if i < len(s)-1 && ans[i+1] == cc {
134-
continue
135-
}
136-
ans[i] = cc
140+
cs[i] = c
137141
break
138142
}
139143
}
140144
}
141-
return string(ans)
145+
return string(cs)
142146
}
143147
```
144148

145149
### **TypeScript**
146150

147151
```ts
148152
function modifyString(s: string): string {
149-
const strArr = s.split('');
153+
const cs = s.split('');
150154
const n = s.length;
151-
for (let i = 0; i < n; i++) {
152-
if (strArr[i] === '?') {
153-
const before = strArr[i - 1];
154-
const after = strArr[i + 1];
155-
156-
if (after !== 'a' && before !== 'a') {
157-
strArr[i] = 'a';
158-
} else if (after !== 'b' && before !== 'b') {
159-
strArr[i] = 'b';
160-
} else {
161-
strArr[i] = 'c';
155+
for (let i = 0; i < n; ++i) {
156+
if (cs[i] === '?') {
157+
for (const c of 'abc') {
158+
if (
159+
(i > 0 && cs[i - 1] === c) ||
160+
(i + 1 < n && cs[i + 1] === c)
161+
) {
162+
continue;
163+
}
164+
cs[i] = c;
165+
break;
162166
}
163167
}
164168
}
165-
return strArr.join('');
169+
return cs.join('');
166170
}
167171
```
168172

173+
### **...**
174+
175+
```
176+
177+
```
178+
169179
<!-- tabs:end -->

solution/1500-1599/1576.Replace All 's to Avoid Consecutive Repeating Characters/README_EN.md

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -44,41 +44,37 @@
4444
```python
4545
class Solution:
4646
def modifyString(self, s: str) -> str:
47-
ans = list(s)
48-
for i, c in enumerate(ans):
49-
if c == '?':
50-
for cc in 'abc':
51-
if i > 0 and ans[i - 1] == cc:
47+
s = list(s)
48+
n = len(s)
49+
for i in range(n):
50+
if s[i] == "?":
51+
for c in "abc":
52+
if (i and s[i - 1] == c) or (i + 1 < n and s[i + 1] == c):
5253
continue
53-
if i < len(s) - 1 and ans[i + 1] == cc:
54-
continue
55-
ans[i] = cc
54+
s[i] = c
5655
break
57-
return ''.join(ans)
56+
return "".join(s)
5857
```
5958

6059
### **Java**
6160

6261
```java
6362
class Solution {
6463
public String modifyString(String s) {
65-
char[] chars = s.toCharArray();
66-
for (int i = 0; i < chars.length; ++i) {
67-
char c = chars[i];
68-
if (c == '?') {
69-
for (char cc = 'a'; cc <= 'c'; ++cc) {
70-
if (i > 0 && chars[i - 1] == cc) {
71-
continue;
72-
}
73-
if (i < chars.length - 1 && chars[i + 1] == cc) {
64+
char[] cs = s.toCharArray();
65+
int n = cs.length;
66+
for (int i = 0; i < n; ++i) {
67+
if (cs[i] == '?') {
68+
for (char c = 'a'; c <= 'c'; ++c) {
69+
if ((i > 0 && cs[i - 1] == c) || (i + 1 < n && cs[i + 1] == c)) {
7470
continue;
7571
}
76-
chars[i] = cc;
72+
cs[i] = c;
7773
break;
7874
}
7975
}
8076
}
81-
return String.valueOf(chars);
77+
return String.valueOf(cs);
8278
}
8379
}
8480
```
@@ -89,12 +85,14 @@ class Solution {
8985
class Solution {
9086
public:
9187
string modifyString(string s) {
92-
for (int i = 0; i < s.size(); ++i) {
88+
int n = s.size();
89+
for (int i = 0; i < n; ++i) {
9390
if (s[i] == '?') {
94-
for (char cc : "abc") {
95-
if (i > 0 && s[i - 1] == cc) continue;
96-
if (i < s.size() - 1 && s[i + 1] == cc) continue;
97-
s[i] = cc;
91+
for (char c : "abc") {
92+
if ((i && s[i - 1] == c) || (i + 1 < n && s[i + 1] == c)) {
93+
continue;
94+
}
95+
s[i] = c;
9896
break;
9997
}
10098
}
@@ -108,47 +106,51 @@ public:
108106
109107
```go
110108
func modifyString(s string) string {
111-
ans := []byte(s)
112-
for i, c := range ans {
113-
if c == '?' {
114-
for cc := byte('a'); cc <= 'c'; cc++ {
115-
if i > 0 && ans[i-1] == cc {
116-
continue
117-
}
118-
if i < len(s)-1 && ans[i+1] == cc {
109+
n := len(s)
110+
cs := []byte(s)
111+
for i := range s {
112+
if cs[i] == '?' {
113+
for c := byte('a'); c <= byte('c'); c++ {
114+
if (i > 0 && cs[i-1] == c) || (i+1 < n && cs[i+1] == c) {
119115
continue
120116
}
121-
ans[i] = cc
117+
cs[i] = c
122118
break
123119
}
124120
}
125121
}
126-
return string(ans)
122+
return string(cs)
127123
}
128124
```
129125

130126
### **TypeScript**
131127

132128
```ts
133129
function modifyString(s: string): string {
134-
const strArr = s.split('');
130+
const cs = s.split('');
135131
const n = s.length;
136-
for (let i = 0; i < n; i++) {
137-
if (strArr[i] === '?') {
138-
const before = strArr[i - 1];
139-
const after = strArr[i + 1];
140-
141-
if (after !== 'a' && before !== 'a') {
142-
strArr[i] = 'a';
143-
} else if (after !== 'b' && before !== 'b') {
144-
strArr[i] = 'b';
145-
} else {
146-
strArr[i] = 'c';
132+
for (let i = 0; i < n; ++i) {
133+
if (cs[i] === '?') {
134+
for (const c of 'abc') {
135+
if (
136+
(i > 0 && cs[i - 1] === c) ||
137+
(i + 1 < n && cs[i + 1] === c)
138+
) {
139+
continue;
140+
}
141+
cs[i] = c;
142+
break;
147143
}
148144
}
149145
}
150-
return strArr.join('');
146+
return cs.join('');
151147
}
152148
```
153149

150+
### **...**
151+
152+
```
153+
154+
```
155+
154156
<!-- tabs:end -->

solution/1500-1599/1576.Replace All 's to Avoid Consecutive Repeating Characters/Solution.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
class Solution {
22
public:
33
string modifyString(string s) {
4-
for (int i = 0; i < s.size(); ++i) {
4+
int n = s.size();
5+
for (int i = 0; i < n; ++i) {
56
if (s[i] == '?') {
6-
for (char cc : "abc") {
7-
if (i > 0 && s[i - 1] == cc) continue;
8-
if (i < s.size() - 1 && s[i + 1] == cc) continue;
9-
s[i] = cc;
7+
for (char c : "abc") {
8+
if ((i && s[i - 1] == c) || (i + 1 < n && s[i + 1] == c)) {
9+
continue;
10+
}
11+
s[i] = c;
1012
break;
1113
}
1214
}
Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
func modifyString(s string) string {
2-
ans := []byte(s)
3-
for i, c := range ans {
4-
if c == '?' {
5-
for cc := byte('a'); cc <= 'c'; cc++ {
6-
if i > 0 && ans[i-1] == cc {
2+
n := len(s)
3+
cs := []byte(s)
4+
for i := range s {
5+
if cs[i] == '?' {
6+
for c := byte('a'); c <= byte('c'); c++ {
7+
if (i > 0 && cs[i-1] == c) || (i+1 < n && cs[i+1] == c) {
78
continue
89
}
9-
if i < len(s)-1 && ans[i+1] == cc {
10-
continue
11-
}
12-
ans[i] = cc
10+
cs[i] = c
1311
break
1412
}
1513
}
1614
}
17-
return string(ans)
15+
return string(cs)
1816
}
Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
class Solution {
22
public String modifyString(String s) {
3-
char[] chars = s.toCharArray();
4-
for (int i = 0; i < chars.length; ++i) {
5-
char c = chars[i];
6-
if (c == '?') {
7-
for (char cc = 'a'; cc <= 'c'; ++cc) {
8-
if (i > 0 && chars[i - 1] == cc) {
3+
char[] cs = s.toCharArray();
4+
int n = cs.length;
5+
for (int i = 0; i < n; ++i) {
6+
if (cs[i] == '?') {
7+
for (char c = 'a'; c <= 'c'; ++c) {
8+
if ((i > 0 && cs[i - 1] == c) || (i + 1 < n && cs[i + 1] == c)) {
99
continue;
1010
}
11-
if (i < chars.length - 1 && chars[i + 1] == cc) {
12-
continue;
13-
}
14-
chars[i] = cc;
11+
cs[i] = c;
1512
break;
1613
}
1714
}
1815
}
19-
return String.valueOf(chars);
16+
return String.valueOf(cs);
2017
}
2118
}

0 commit comments

Comments
 (0)