Skip to content

Commit 10dac3e

Browse files
committed
feat: add solutions to lc problem: No.2038
No.2038.Remove Colored Pieces if Both Neighbors are the Same Color
1 parent 839f063 commit 10dac3e

File tree

6 files changed

+142
-157
lines changed

6 files changed

+142
-157
lines changed

Diff for: solution/2000-2099/2038.Remove Colored Pieces if Both Neighbors are the Same Color/README.md

+52-53
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,13 @@ ABBBB<strong><em>B</em></strong>BBAA -&gt; ABBBBBBAA
7575

7676
<!-- 这里可写通用的实现逻辑 -->
7777

78-
统计字符串中连续出现 3 个 'A' 或 3 个 'B' 的个数,分别记为 cnt1, cnt2。只要 cnt1 大于 cnt2,返回 true,否则返回 false。
78+
**方法一:计数**
79+
80+
统计字符串 `colors` 中连续出现 $3$ 个 `'A'` 或 $3$ 个 `'B'` 的个数,分别记为 $a$ 和 $b$。
81+
82+
最后判断 $a$ 是否大于 $b$,是则返回 `true`,否则返回 `false`
83+
84+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 `colors` 的长度。
7985

8086
<!-- tabs:start -->
8187

@@ -87,19 +93,13 @@ ABBBB<strong><em>B</em></strong>BBAA -&gt; ABBBBBBAA
8793
class Solution:
8894
def winnerOfGame(self, colors: str) -> bool:
8995
a = b = 0
90-
cnt1 = cnt2 = 0
91-
for c in colors:
92-
if c == 'A':
93-
a += 1
94-
if a > 2:
95-
cnt1 += 1
96-
b = 0
97-
else:
98-
b += 1
99-
if b > 2:
100-
cnt2 += 1
101-
a = 0
102-
return cnt1 > cnt2
96+
for c, v in groupby(colors):
97+
m = len(list(v)) - 2
98+
if m > 0 and c == 'A':
99+
a += m
100+
elif m > 0 and c == 'B':
101+
b += m
102+
return a > b
103103
```
104104

105105
### **Java**
@@ -109,24 +109,22 @@ class Solution:
109109
```java
110110
class Solution {
111111
public boolean winnerOfGame(String colors) {
112+
int n = colors.length();
112113
int a = 0, b = 0;
113-
int cnt1 = 0, cnt2 = 0;
114-
for (char c : colors.toCharArray()) {
115-
if (c == 'A') {
116-
++a;
117-
if (a > 2) {
118-
++cnt1;
119-
}
120-
b = 0;
121-
} else {
122-
++b;
123-
if (b > 2) {
124-
++cnt2;
114+
for (int i = 0, j = 0; i < n; i = j) {
115+
while (j < n && colors.charAt(j) == colors.charAt(i)) {
116+
++j;
117+
}
118+
int m = j - i - 2;
119+
if (m > 0) {
120+
if (colors.charAt(i) == 'A') {
121+
a += m;
122+
} else {
123+
b += m;
125124
}
126-
a = 0;
127125
}
128126
}
129-
return cnt1 > cnt2;
127+
return a > b;
130128
}
131129
}
132130
```
@@ -137,20 +135,22 @@ class Solution {
137135
class Solution {
138136
public:
139137
bool winnerOfGame(string colors) {
138+
int n = colors.size();
140139
int a = 0, b = 0;
141-
int cnt1 = 0, cnt2 = 0;
142-
for (char& c : colors) {
143-
if (c == 'A') {
144-
++a;
145-
if (a > 2) ++cnt1;
146-
b = 0;
147-
} else {
148-
++b;
149-
if (b > 2) ++cnt2;
150-
a = 0;
140+
for (int i = 0, j = 0; i < n; i = j) {
141+
while (j < n && colors[j] == colors[i]) {
142+
++j;
143+
}
144+
int m = j - i - 2;
145+
if (m > 0) {
146+
if (colors[i] == 'A') {
147+
a += m;
148+
} else {
149+
b += m;
150+
}
151151
}
152152
}
153-
return cnt1 > cnt2;
153+
return a > b;
154154
}
155155
};
156156
```
@@ -159,23 +159,22 @@ public:
159159
160160
```go
161161
func winnerOfGame(colors string) bool {
162-
var a, b, cnt1, cnt2 int
163-
for _, c := range colors {
164-
if c == 'A' {
165-
a++
166-
if a > 2 {
167-
cnt1++
168-
}
169-
b = 0
170-
} else {
171-
b++
172-
if b > 2 {
173-
cnt2++
162+
n := len(colors)
163+
a, b := 0, 0
164+
for i, j := 0, 0; i < n; i = j {
165+
for j < n && colors[j] == colors[i] {
166+
j++
167+
}
168+
m := j - i - 2
169+
if m > 0 {
170+
if colors[i] == 'A' {
171+
a += m
172+
} else {
173+
b += m
174174
}
175-
a = 0
176175
}
177176
}
178-
return cnt1 > cnt2
177+
return a > b
179178
}
180179
```
181180

Diff for: solution/2000-2099/2038.Remove Colored Pieces if Both Neighbors are the Same Color/README_EN.md

+45-52
Original file line numberDiff line numberDiff line change
@@ -80,44 +80,36 @@ Thus, Bob wins, so return false.
8080
class Solution:
8181
def winnerOfGame(self, colors: str) -> bool:
8282
a = b = 0
83-
cnt1 = cnt2 = 0
84-
for c in colors:
85-
if c == 'A':
86-
a += 1
87-
if a > 2:
88-
cnt1 += 1
89-
b = 0
90-
else:
91-
b += 1
92-
if b > 2:
93-
cnt2 += 1
94-
a = 0
95-
return cnt1 > cnt2
83+
for c, v in groupby(colors):
84+
m = len(list(v)) - 2
85+
if m > 0 and c == 'A':
86+
a += m
87+
elif m > 0 and c == 'B':
88+
b += m
89+
return a > b
9690
```
9791

9892
### **Java**
9993

10094
```java
10195
class Solution {
10296
public boolean winnerOfGame(String colors) {
97+
int n = colors.length();
10398
int a = 0, b = 0;
104-
int cnt1 = 0, cnt2 = 0;
105-
for (char c : colors.toCharArray()) {
106-
if (c == 'A') {
107-
++a;
108-
if (a > 2) {
109-
++cnt1;
110-
}
111-
b = 0;
112-
} else {
113-
++b;
114-
if (b > 2) {
115-
++cnt2;
99+
for (int i = 0, j = 0; i < n; i = j) {
100+
while (j < n && colors.charAt(j) == colors.charAt(i)) {
101+
++j;
102+
}
103+
int m = j - i - 2;
104+
if (m > 0) {
105+
if (colors.charAt(i) == 'A') {
106+
a += m;
107+
} else {
108+
b += m;
116109
}
117-
a = 0;
118110
}
119111
}
120-
return cnt1 > cnt2;
112+
return a > b;
121113
}
122114
}
123115
```
@@ -128,20 +120,22 @@ class Solution {
128120
class Solution {
129121
public:
130122
bool winnerOfGame(string colors) {
123+
int n = colors.size();
131124
int a = 0, b = 0;
132-
int cnt1 = 0, cnt2 = 0;
133-
for (char& c : colors) {
134-
if (c == 'A') {
135-
++a;
136-
if (a > 2) ++cnt1;
137-
b = 0;
138-
} else {
139-
++b;
140-
if (b > 2) ++cnt2;
141-
a = 0;
125+
for (int i = 0, j = 0; i < n; i = j) {
126+
while (j < n && colors[j] == colors[i]) {
127+
++j;
128+
}
129+
int m = j - i - 2;
130+
if (m > 0) {
131+
if (colors[i] == 'A') {
132+
a += m;
133+
} else {
134+
b += m;
135+
}
142136
}
143137
}
144-
return cnt1 > cnt2;
138+
return a > b;
145139
}
146140
};
147141
```
@@ -150,23 +144,22 @@ public:
150144
151145
```go
152146
func winnerOfGame(colors string) bool {
153-
var a, b, cnt1, cnt2 int
154-
for _, c := range colors {
155-
if c == 'A' {
156-
a++
157-
if a > 2 {
158-
cnt1++
159-
}
160-
b = 0
161-
} else {
162-
b++
163-
if b > 2 {
164-
cnt2++
147+
n := len(colors)
148+
a, b := 0, 0
149+
for i, j := 0, 0; i < n; i = j {
150+
for j < n && colors[j] == colors[i] {
151+
j++
152+
}
153+
m := j - i - 2
154+
if m > 0 {
155+
if colors[i] == 'A' {
156+
a += m
157+
} else {
158+
b += m
165159
}
166-
a = 0
167160
}
168161
}
169-
return cnt1 > cnt2
162+
return a > b
170163
}
171164
```
172165

Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
class Solution {
22
public:
33
bool winnerOfGame(string colors) {
4+
int n = colors.size();
45
int a = 0, b = 0;
5-
int cnt1 = 0, cnt2 = 0;
6-
for (char& c : colors) {
7-
if (c == 'A') {
8-
++a;
9-
if (a > 2) ++cnt1;
10-
b = 0;
11-
} else {
12-
++b;
13-
if (b > 2) ++cnt2;
14-
a = 0;
6+
for (int i = 0, j = 0; i < n; i = j) {
7+
while (j < n && colors[j] == colors[i]) {
8+
++j;
9+
}
10+
int m = j - i - 2;
11+
if (m > 0) {
12+
if (colors[i] == 'A') {
13+
a += m;
14+
} else {
15+
b += m;
16+
}
1517
}
1618
}
17-
return cnt1 > cnt2;
19+
return a > b;
1820
}
1921
};
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
func winnerOfGame(colors string) bool {
2-
var a, b, cnt1, cnt2 int
3-
for _, c := range colors {
4-
if c == 'A' {
5-
a++
6-
if a > 2 {
7-
cnt1++
8-
}
9-
b = 0
10-
} else {
11-
b++
12-
if b > 2 {
13-
cnt2++
2+
n := len(colors)
3+
a, b := 0, 0
4+
for i, j := 0, 0; i < n; i = j {
5+
for j < n && colors[j] == colors[i] {
6+
j++
7+
}
8+
m := j - i - 2
9+
if m > 0 {
10+
if colors[i] == 'A' {
11+
a += m
12+
} else {
13+
b += m
1414
}
15-
a = 0
1615
}
1716
}
18-
return cnt1 > cnt2
17+
return a > b
1918
}
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
class Solution {
22
public boolean winnerOfGame(String colors) {
3+
int n = colors.length();
34
int a = 0, b = 0;
4-
int cnt1 = 0, cnt2 = 0;
5-
for (char c : colors.toCharArray()) {
6-
if (c == 'A') {
7-
++a;
8-
if (a > 2) {
9-
++cnt1;
10-
}
11-
b = 0;
12-
} else {
13-
++b;
14-
if (b > 2) {
15-
++cnt2;
5+
for (int i = 0, j = 0; i < n; i = j) {
6+
while (j < n && colors.charAt(j) == colors.charAt(i)) {
7+
++j;
8+
}
9+
int m = j - i - 2;
10+
if (m > 0) {
11+
if (colors.charAt(i) == 'A') {
12+
a += m;
13+
} else {
14+
b += m;
1615
}
17-
a = 0;
1816
}
1917
}
20-
return cnt1 > cnt2;
18+
return a > b;
2119
}
2220
}

0 commit comments

Comments
 (0)