Skip to content

Commit 198140f

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 370d053 commit 198140f

File tree

3 files changed

+106
-6
lines changed

3 files changed

+106
-6
lines changed

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

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ ABBBB<strong><em>B</em></strong>BBAA -&gt; ABBBBBBAA
9494

9595
### 方法一:计数
9696

97-
我们统计字符串 `colors` 中连续出现 $3$ 个 `'A'` 或 $3$ 个 `'B'` 的个数,分别记为 $a$ 和 $b$。
97+
我们可以遍历字符串 $\textit{colors}$,找出每一个连续出现的字符段,记该字符串长度减去 $2$ 的值为 $m$,如果该段字符是 `'A'`,则将 $m$ 加入计数器 $a$,如果该段字符是 `'B'`,则将 $m$ 加入计数器 $b$。
9898

99-
最后判断 $a$ 是否大于 $b$,是则返回 `true`,否则返回 `false`
99+
最后判断 $a$ 是否大于 $b$,如果是,则 Alice 获胜,返回 $\textit{true}$;否则 Bob 获胜,返回 $\textit{false}$
100100

101-
时间复杂度 $O(n)$,其中 $n$ 为字符串 `colors` 的长度。空间复杂度 $O(1)$。
101+
时间复杂度 $O(n)$,其中 $n$ 为字符串 $\textit{colors}$ 的长度。空间复杂度 $O(1)$。
102102

103103
<!-- tabs:start -->
104104

@@ -214,6 +214,39 @@ function winnerOfGame(colors: string): boolean {
214214
}
215215
```
216216

217+
#### Rust
218+
219+
```rust
220+
impl Solution {
221+
pub fn winner_of_game(colors: String) -> bool {
222+
let n = colors.len();
223+
let s = colors.as_bytes();
224+
let mut a = 0i32;
225+
let mut b = 0i32;
226+
let mut i = 0usize;
227+
let mut j;
228+
229+
while i < n {
230+
j = i;
231+
while j < n && s[j] == s[i] {
232+
j += 1;
233+
}
234+
let m = j as i32 - i as i32 - 2;
235+
if m > 0 {
236+
if s[i] == b'A' {
237+
a += m;
238+
} else {
239+
b += m;
240+
}
241+
}
242+
i = j;
243+
}
244+
245+
a > b
246+
}
247+
}
248+
```
249+
217250
<!-- tabs:end -->
218251

219252
<!-- solution:end -->

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

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,17 @@ Thus, Bob wins, so return false.
9595

9696
### Solution 1: Counting
9797

98-
We count the number of times that the string `colors` contains three consecutive `'A'`s or three consecutive `'B'`s, denoted as $a$ and $b$, respectively.
98+
We can traverse the string $\textit{colors}$ and identify every contiguous block of identical characters. For each block, let its length minus $2$ be $m$.
9999

100-
Finally, we check whether $a$ is greater than $b$. If it is, we return `true`. Otherwise, we return `false`.
100+
- If the block consists of `'A'`, add $m$ to counter $a$.
101+
- If the block consists of `'B'`, add $m$ to counter $b$.
101102

102-
The time complexity is $O(n)$, where $n$ is the length of the string `colors`. The space complexity is $O(1)$.
103+
Finally, check whether $a > b$.
104+
105+
- If so, Alice wins and we return $\textit{true}$.
106+
- Otherwise, Bob wins and we return $\textit{false}$.
107+
108+
The time complexity is $O(n)$, where $n$ is the length of $\textit{colors}$, and the space complexity is $O(1)$.
103109

104110
<!-- tabs:start -->
105111

@@ -215,6 +221,39 @@ function winnerOfGame(colors: string): boolean {
215221
}
216222
```
217223

224+
#### Rust
225+
226+
```rust
227+
impl Solution {
228+
pub fn winner_of_game(colors: String) -> bool {
229+
let n = colors.len();
230+
let s = colors.as_bytes();
231+
let mut a = 0i32;
232+
let mut b = 0i32;
233+
let mut i = 0usize;
234+
let mut j;
235+
236+
while i < n {
237+
j = i;
238+
while j < n && s[j] == s[i] {
239+
j += 1;
240+
}
241+
let m = j as i32 - i as i32 - 2;
242+
if m > 0 {
243+
if s[i] == b'A' {
244+
a += m;
245+
} else {
246+
b += m;
247+
}
248+
}
249+
i = j;
250+
}
251+
252+
a > b
253+
}
254+
}
255+
```
256+
218257
<!-- tabs:end -->
219258

220259
<!-- solution:end -->
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
impl Solution {
2+
pub fn winner_of_game(colors: String) -> bool {
3+
let n = colors.len();
4+
let s = colors.as_bytes();
5+
let mut a = 0i32;
6+
let mut b = 0i32;
7+
let mut i = 0usize;
8+
let mut j;
9+
10+
while i < n {
11+
j = i;
12+
while j < n && s[j] == s[i] {
13+
j += 1;
14+
}
15+
let m = j as i32 - i as i32 - 2;
16+
if m > 0 {
17+
if s[i] == b'A' {
18+
a += m;
19+
} else {
20+
b += m;
21+
}
22+
}
23+
i = j;
24+
}
25+
26+
a > b
27+
}
28+
}

0 commit comments

Comments
 (0)