Skip to content

Commit b10922c

Browse files
authored
feat: add weekly contest 372 (#1983)
1 parent 935ac39 commit b10922c

File tree

14 files changed

+729
-1
lines changed

14 files changed

+729
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# [2937. 使三个字符串相等](https://leetcode.cn/problems/make-three-strings-equal)
2+
3+
[English Version](/solution/2900-2999/2937.Make%20Three%20Strings%20Equal/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你三个字符串 <code>s1</code>、<code>s2</code> 和 <code>s3</code>。 你可以根据需要对这三个字符串执行以下操作 <strong>任意次数</strong> <!-- notionvc: b5178de7-3318-4129-b7d9-726b47e90621 -->。</p>
10+
11+
<p>在每次操作中,你可以选择其中一个长度至少为 <code>2</code> 的字符串 <!-- notionvc: 3342ac46-33c8-4010-aacd-e58678ce31ef --> 并删除其 <strong>最右位置上</strong> 的字符。</p>
12+
13+
<p>如果存在某种方法能够使这三个字符串相等,请返回使它们相等所需的 <strong>最小</strong> 操作次数;否则,返回 <code>-1</code>。</p>
14+
15+
<p>&nbsp;</p>
16+
17+
<p><strong class="example">示例 1:</strong></p>
18+
19+
<pre>
20+
<strong>输入:</strong>s1 = "abc",s2 = "abb",s3 = "ab"
21+
<strong>输出:</strong>2
22+
<strong>解释:</strong>对 s1 和 s2 进行一次操作后,可以得到三个相等的字符串。
23+
可以证明,不可能用少于两次操作使它们相等。</pre>
24+
25+
<p><strong class="example">示例 2:</strong></p>
26+
27+
<pre>
28+
<strong>输入:</strong>s1 = "dac",s2 = "bac",s3 = "cac"
29+
<strong>输出:</strong>-1
30+
<strong>解释:</strong>因为 s1 和 s2 的最左位置上的字母<!-- notionvc: 47239f7c-eec1-49f8-af79-c206ec88cb07 -->不相等,所以无论进行多少次操作,它们都不可能相等。因此答案是 -1 。</pre>
31+
32+
<p>&nbsp;</p>
33+
34+
<p><strong>提示:</strong></p>
35+
36+
<ul>
37+
<li><code>1 &lt;= s1.length, s2.length, s3.length &lt;= 100</code></li>
38+
<li><code>s1</code>、<code>s2</code> 和 <code>s3</code> 仅由小写英文字母组成。</li>
39+
</ul>
40+
41+
## 解法
42+
43+
<!-- 这里可写通用的实现逻辑 -->
44+
45+
<!-- tabs:start -->
46+
47+
### **Python3**
48+
49+
<!-- 这里可写当前语言的特殊实现逻辑 -->
50+
51+
```python
52+
53+
```
54+
55+
### **Java**
56+
57+
<!-- 这里可写当前语言的特殊实现逻辑 -->
58+
59+
```java
60+
61+
```
62+
63+
### **C++**
64+
65+
```cpp
66+
67+
```
68+
69+
### **Go**
70+
71+
```go
72+
73+
```
74+
75+
### **...**
76+
77+
```
78+
79+
```
80+
81+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# [2937. Make Three Strings Equal](https://leetcode.com/problems/make-three-strings-equal)
2+
3+
[中文文档](/solution/2900-2999/2937.Make%20Three%20Strings%20Equal/README.md)
4+
5+
## Description
6+
7+
<p>You are given three strings <code>s1</code>, <code>s2</code>, and <code>s3</code>. You have to perform the following operation on these three strings <strong>as many times</strong> as you want<!-- notionvc: b5178de7-3318-4129-b7d9-726b47e90621 -->.</p>
8+
9+
<p>In one operation you can choose one of these three strings such that its length is at least <code>2</code><!-- notionvc: 3342ac46-33c8-4010-aacd-e58678ce31ef --> and delete the <strong>rightmost</strong> character of it.</p>
10+
11+
<p>Return <em>the <strong>minimum</strong> number of operations you need to perform to make the three strings equal if there is a way to make them equal, otherwise, return </em><code>-1</code><em>.</em></p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong class="example">Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> s1 = &quot;abc&quot;, s2 = &quot;abb&quot;, s3 = &quot;ab&quot;
18+
<strong>Output:</strong> 2
19+
<strong>Explanation:</strong> Performing operations on s1 and s2 once will lead to three equal strings.
20+
It can be shown that there is no way to make them equal with less than two operations.</pre>
21+
22+
<p><strong class="example">Example 2:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> s1 = &quot;dac&quot;, s2 = &quot;bac&quot;, s3 = &quot;cac&quot;
26+
<strong>Output:</strong> -1
27+
<strong>Explanation:</strong> Because the leftmost letters<!-- notionvc: 47239f7c-eec1-49f8-af79-c206ec88cb07 --> of s1 and s2 are not equal, they could not be equal after any number of operations. So the answer is -1.
28+
</pre>
29+
30+
<p>&nbsp;</p>
31+
<p><strong>Constraints:</strong></p>
32+
33+
<ul>
34+
<li><code>1 &lt;= s1.length, s2.length, s3.length &lt;= 100</code></li>
35+
<li><font face="monospace"><code>s1</code>,</font> <code><font face="monospace">s2</font></code><font face="monospace"> and</font> <code><font face="monospace">s3</font></code> consist only of lowercase English letters.</li>
36+
</ul>
37+
38+
## Solutions
39+
40+
<!-- tabs:start -->
41+
42+
### **Python3**
43+
44+
```python
45+
46+
```
47+
48+
### **Java**
49+
50+
```java
51+
52+
```
53+
54+
### **C++**
55+
56+
```cpp
57+
58+
```
59+
60+
### **Go**
61+
62+
```go
63+
64+
```
65+
66+
### **...**
67+
68+
```
69+
70+
```
71+
72+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# [2938. 区分黑球与白球](https://leetcode.cn/problems/separate-black-and-white-balls)
2+
3+
[English Version](/solution/2900-2999/2938.Separate%20Black%20and%20White%20Balls/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>桌子上有 <code>n</code> 个球,每个球的颜色不是黑色,就是白色。</p>
10+
11+
<p>给你一个长度为 <code>n</code> 、下标从 <strong>0</strong> 开始的二进制字符串 <code>s</code>,其中 <code>1</code> 和 <code>0</code> 分别代表黑色和白色的球。</p>
12+
13+
<p>在每一步中,你可以选择两个相邻的球并交换它们。</p>
14+
15+
<p>返回「将所有黑色球都移到右侧,所有白色球都移到左侧所需的 <strong>最小步数</strong>」。</p>
16+
17+
<p>&nbsp;</p>
18+
19+
<p><strong class="example">示例 1:</strong></p>
20+
21+
<pre>
22+
<strong>输入:</strong>s = "101"
23+
<strong>输出:</strong>1
24+
<strong>解释:</strong>我们可以按以下方式将所有黑色球移到右侧:
25+
- 交换 s[0] 和 s[1],s = "011"。
26+
最开始,1 没有都在右侧,需要至少 1 步将其移到右侧。</pre>
27+
28+
<p><strong class="example">示例 2:</strong></p>
29+
30+
<pre>
31+
<strong>输入:</strong>s = "100"
32+
<strong>输出:</strong>2
33+
<strong>解释:</strong>我们可以按以下方式将所有黑色球移到右侧:
34+
- 交换 s[0] 和 s[1],s = "010"。
35+
- 交换 s[1] 和 s[2],s = "001"。
36+
可以证明所需的最小步数为 2 。
37+
</pre>
38+
39+
<p><strong class="example">示例 3:</strong></p>
40+
41+
<pre>
42+
<strong>输入:</strong>s = "0111"
43+
<strong>输出:</strong>0
44+
<strong>解释:</strong>所有黑色球都已经在右侧。
45+
</pre>
46+
47+
<p>&nbsp;</p>
48+
49+
<p><strong>提示:</strong></p>
50+
51+
<ul>
52+
<li><code>1 &lt;= n == s.length &lt;= 10<sup>5</sup></code></li>
53+
<li><code>s[i]</code> 不是 <code>'0'</code>,就是 <code>'1'</code>。</li>
54+
</ul>
55+
56+
## 解法
57+
58+
<!-- 这里可写通用的实现逻辑 -->
59+
60+
<!-- tabs:start -->
61+
62+
### **Python3**
63+
64+
<!-- 这里可写当前语言的特殊实现逻辑 -->
65+
66+
```python
67+
68+
```
69+
70+
### **Java**
71+
72+
<!-- 这里可写当前语言的特殊实现逻辑 -->
73+
74+
```java
75+
76+
```
77+
78+
### **C++**
79+
80+
```cpp
81+
82+
```
83+
84+
### **Go**
85+
86+
```go
87+
88+
```
89+
90+
### **...**
91+
92+
```
93+
94+
```
95+
96+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# [2938. Separate Black and White Balls](https://leetcode.com/problems/separate-black-and-white-balls)
2+
3+
[中文文档](/solution/2900-2999/2938.Separate%20Black%20and%20White%20Balls/README.md)
4+
5+
## Description
6+
7+
<p>There are <code>n</code> balls on a table, each ball has a color black or white.</p>
8+
9+
<p>You are given a <strong>0-indexed</strong> binary string <code>s</code> of length <code>n</code>, where <code>1</code> and <code>0</code> represent black and white balls, respectively.</p>
10+
11+
<p>In each step, you can choose two adjacent balls and swap them.</p>
12+
13+
<p>Return <em>the <strong>minimum</strong> number of steps to group all the black balls to the right and all the white balls to the left</em>.</p>
14+
15+
<p>&nbsp;</p>
16+
<p><strong class="example">Example 1:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> s = &quot;101&quot;
20+
<strong>Output:</strong> 1
21+
<strong>Explanation:</strong> We can group all the black balls to the right in the following way:
22+
- Swap s[0] and s[1], s = &quot;011&quot;.
23+
Initially, 1s are not grouped together, requiring at least 1 step to group them to the right.</pre>
24+
25+
<p><strong class="example">Example 2:</strong></p>
26+
27+
<pre>
28+
<strong>Input:</strong> s = &quot;100&quot;
29+
<strong>Output:</strong> 2
30+
<strong>Explanation:</strong> We can group all the black balls to the right in the following way:
31+
- Swap s[0] and s[1], s = &quot;010&quot;.
32+
- Swap s[1] and s[2], s = &quot;001&quot;.
33+
It can be proven that the minimum number of steps needed is 2.
34+
</pre>
35+
36+
<p><strong class="example">Example 3:</strong></p>
37+
38+
<pre>
39+
<strong>Input:</strong> s = &quot;0111&quot;
40+
<strong>Output:</strong> 0
41+
<strong>Explanation:</strong> All the black balls are already grouped to the right.
42+
</pre>
43+
44+
<p>&nbsp;</p>
45+
<p><strong>Constraints:</strong></p>
46+
47+
<ul>
48+
<li><code>1 &lt;= n == s.length &lt;= 10<sup>5</sup></code></li>
49+
<li><code>s[i]</code> is either <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li>
50+
</ul>
51+
52+
## Solutions
53+
54+
<!-- tabs:start -->
55+
56+
### **Python3**
57+
58+
```python
59+
60+
```
61+
62+
### **Java**
63+
64+
```java
65+
66+
```
67+
68+
### **C++**
69+
70+
```cpp
71+
72+
```
73+
74+
### **Go**
75+
76+
```go
77+
78+
```
79+
80+
### **...**
81+
82+
```
83+
84+
```
85+
86+
<!-- tabs:end -->

0 commit comments

Comments
 (0)