Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bc650d2

Browse files
feat: add solutions to lc problems: No.2937,2938 (#1985)
* No.2937.Make Three Strings Equal * No.2938.Separate Black and White Balls
1 parent a4fc5c0 commit bc650d2

File tree

14 files changed

+392
-12
lines changed

14 files changed

+392
-12
lines changed

‎solution/2900-2999/2937.Make Three Strings Equal/README.md‎

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,34 +42,97 @@
4242

4343
<!-- 这里可写通用的实现逻辑 -->
4444

45+
**方法一:枚举**
46+
47+
根据题目描述,我们知道,如果删除字符后的三个字符串相等,那么它们存在一个长度大于 1ドル$ 的公共前缀。因此,我们可以枚举公共前缀的位置 $i,ドル如果当前下标 $i$ 对应的三个字符不完全相等,那么公共前缀长度为 $i,ドル此时,我们判断 $i$ 是否为 0ドル,ドル若是,返回 $-1,ドル否则返回 $s - 3 \times i,ドル其中 $s$ 为三个字符串的长度和。
48+
49+
时间复杂度 $O(n),ドル其中 $n$ 为三个字符串的最小长度。空间复杂度 $O(1)$。
50+
4551
<!-- tabs:start -->
4652

4753
### **Python3**
4854

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

5157
```python
52-
58+
class Solution:
59+
def findMinimumOperations(self, s1: str, s2: str, s3: str) -> int:
60+
s = len(s1) + len(s2) + len(s3)
61+
n = min(len(s1), len(s2), len(s3))
62+
for i in range(n):
63+
if not s1[i] == s2[i] == s3[i]:
64+
return -1 if i == 0 else s - 3 * i
65+
return s - 3 * n
5366
```
5467

5568
### **Java**
5669

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

5972
```java
60-
73+
class Solution {
74+
public int findMinimumOperations(String s1, String s2, String s3) {
75+
int s = s1.length() + s2.length() + s3.length();
76+
int n = Math.min(Math.min(s1.length(), s2.length()), s3.length());
77+
for (int i = 0; i < n; ++i) {
78+
if (!(s1.charAt(i) == s2.charAt(i) && s2.charAt(i) == s3.charAt(i))) {
79+
return i == 0 ? -1 : s - 3 * i;
80+
}
81+
}
82+
return s - 3 * n;
83+
}
84+
}
6185
```
6286

6387
### **C++**
6488

6589
```cpp
66-
90+
class Solution {
91+
public:
92+
int findMinimumOperations(string s1, string s2, string s3) {
93+
int s = s1.size() + s2.size() + s3.size();
94+
int n = min({s1.size(), s2.size(), s3.size()});
95+
for (int i = 0; i < n; ++i) {
96+
if (!(s1[i] == s2[i] && s2[i] == s3[i])) {
97+
return i == 0 ? -1 : s - 3 * i;
98+
}
99+
}
100+
return s - 3 * n;
101+
}
102+
};
67103
```
68104
69105
### **Go**
70106
71107
```go
108+
func findMinimumOperations(s1 string, s2 string, s3 string) int {
109+
s := len(s1) + len(s2) + len(s3)
110+
n := min(len(s1), len(s2), len(s3))
111+
for i := range s1[:n] {
112+
if !(s1[i] == s2[i] && s2[i] == s3[i]) {
113+
if i == 0 {
114+
return -1
115+
}
116+
return s - 3*i
117+
}
118+
}
119+
return s - 3*n
120+
}
121+
```
72122

123+
### **TypeScript**
124+
125+
```ts
126+
function findMinimumOperations(s1: string, s2: string, s3: string): number {
127+
const s = s1.length + s2.length + s3.length;
128+
const n = Math.min(s1.length, s2.length, s3.length);
129+
for (let i = 0; i < n; ++i) {
130+
if (!(s1[i] === s2[i] && s2[i] === s3[i])) {
131+
return i === 0 ? -1 : s - 3 * i;
132+
}
133+
}
134+
return s - 3 * n;
135+
}
73136
```
74137

75138
### **...**

‎solution/2900-2999/2937.Make Three Strings Equal/README_EN.md‎

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,30 +37,93 @@ It can be shown that there is no way to make them equal with less than two opera
3737

3838
## Solutions
3939

40+
**Solution 1: Enumeration**
41+
42+
According to the problem description, we know that if the three strings are equal after deleting characters, then they have a common prefix of length greater than 1ドル$. Therefore, we can enumerate the position $i$ of the common prefix. If the three characters at the current index $i$ are not all equal, then the length of the common prefix is $i$. At this point, we check if $i$ is 0ドル$. If it is, return $-1$. Otherwise, return $s - 3 \times i,ドル where $s$ is the sum of the lengths of the three strings.
43+
44+
The time complexity is $O(n),ドル where $n$ is the minimum length of the three strings. The space complexity is $O(1)$.
45+
4046
<!-- tabs:start -->
4147

4248
### **Python3**
4349

4450
```python
45-
51+
class Solution:
52+
def findMinimumOperations(self, s1: str, s2: str, s3: str) -> int:
53+
s = len(s1) + len(s2) + len(s3)
54+
n = min(len(s1), len(s2), len(s3))
55+
for i in range(n):
56+
if not s1[i] == s2[i] == s3[i]:
57+
return -1 if i == 0 else s - 3 * i
58+
return s - 3 * n
4659
```
4760

4861
### **Java**
4962

5063
```java
51-
64+
class Solution {
65+
public int findMinimumOperations(String s1, String s2, String s3) {
66+
int s = s1.length() + s2.length() + s3.length();
67+
int n = Math.min(Math.min(s1.length(), s2.length()), s3.length());
68+
for (int i = 0; i < n; ++i) {
69+
if (!(s1.charAt(i) == s2.charAt(i) && s2.charAt(i) == s3.charAt(i))) {
70+
return i == 0 ? -1 : s - 3 * i;
71+
}
72+
}
73+
return s - 3 * n;
74+
}
75+
}
5276
```
5377

5478
### **C++**
5579

5680
```cpp
57-
81+
class Solution {
82+
public:
83+
int findMinimumOperations(string s1, string s2, string s3) {
84+
int s = s1.size() + s2.size() + s3.size();
85+
int n = min({s1.size(), s2.size(), s3.size()});
86+
for (int i = 0; i < n; ++i) {
87+
if (!(s1[i] == s2[i] && s2[i] == s3[i])) {
88+
return i == 0 ? -1 : s - 3 * i;
89+
}
90+
}
91+
return s - 3 * n;
92+
}
93+
};
5894
```
5995
6096
### **Go**
6197
6298
```go
99+
func findMinimumOperations(s1 string, s2 string, s3 string) int {
100+
s := len(s1) + len(s2) + len(s3)
101+
n := min(len(s1), len(s2), len(s3))
102+
for i := range s1[:n] {
103+
if !(s1[i] == s2[i] && s2[i] == s3[i]) {
104+
if i == 0 {
105+
return -1
106+
}
107+
return s - 3*i
108+
}
109+
}
110+
return s - 3*n
111+
}
112+
```
63113

114+
### **TypeScript**
115+
116+
```ts
117+
function findMinimumOperations(s1: string, s2: string, s3: string): number {
118+
const s = s1.length + s2.length + s3.length;
119+
const n = Math.min(s1.length, s2.length, s3.length);
120+
for (let i = 0; i < n; ++i) {
121+
if (!(s1[i] === s2[i] && s2[i] === s3[i])) {
122+
return i === 0 ? -1 : s - 3 * i;
123+
}
124+
}
125+
return s - 3 * n;
126+
}
64127
```
65128

66129
### **...**
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int findMinimumOperations(string s1, string s2, string s3) {
4+
int s = s1.size() + s2.size() + s3.size();
5+
int n = min({s1.size(), s2.size(), s3.size()});
6+
for (int i = 0; i < n; ++i) {
7+
if (!(s1[i] == s2[i] && s2[i] == s3[i])) {
8+
return i == 0 ? -1 : s - 3 * i;
9+
}
10+
}
11+
return s - 3 * n;
12+
}
13+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func findMinimumOperations(s1 string, s2 string, s3 string) int {
2+
s := len(s1) + len(s2) + len(s3)
3+
n := min(len(s1), len(s2), len(s3))
4+
for i := range s1[:n] {
5+
if !(s1[i] == s2[i] && s2[i] == s3[i]) {
6+
if i == 0 {
7+
return -1
8+
}
9+
return s - 3*i
10+
}
11+
}
12+
return s - 3*n
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public int findMinimumOperations(String s1, String s2, String s3) {
3+
int s = s1.length() + s2.length() + s3.length();
4+
int n = Math.min(Math.min(s1.length(), s2.length()), s3.length());
5+
for (int i = 0; i < n; ++i) {
6+
if (!(s1.charAt(i) == s2.charAt(i) && s2.charAt(i) == s3.charAt(i))) {
7+
return i == 0 ? -1 : s - 3 * i;
8+
}
9+
}
10+
return s - 3 * n;
11+
}
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def findMinimumOperations(self, s1: str, s2: str, s3: str) -> int:
3+
s = len(s1) + len(s2) + len(s3)
4+
n = min(len(s1), len(s2), len(s3))
5+
for i in range(n):
6+
if not s1[i] == s2[i] == s3[i]:
7+
return -1 if i == 0 else s - 3 * i
8+
return s - 3 * n
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function findMinimumOperations(s1: string, s2: string, s3: string): number {
2+
const s = s1.length + s2.length + s3.length;
3+
const n = Math.min(s1.length, s2.length, s3.length);
4+
for (let i = 0; i < n; ++i) {
5+
if (!(s1[i] === s2[i] && s2[i] === s3[i])) {
6+
return i === 0 ? -1 : s - 3 * i;
7+
}
8+
}
9+
return s - 3 * n;
10+
}

‎solution/2900-2999/2938.Separate Black and White Balls/README.md‎

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,34 +57,103 @@
5757

5858
<!-- 这里可写通用的实现逻辑 -->
5959

60+
**方法一:计数模拟**
61+
62+
我们考虑将所有的 1ドル$ 移到最右边,用一个变量 $cnt$ 记录当前已经移动到最右边的 1ドル$ 的个数,用一个变量 $ans$ 记录移动的次数。
63+
64+
我们从右往左遍历字符串,如果当前位置是 1ドル,ドル那么我们将 $cnt$ 加一,同时将 $n - i - cnt$ 加到 $ans$ 中,其中 $n$ 是字符串的长度。最后返回 $ans$ 即可。
65+
66+
时间复杂度 $O(n),ドル其中 $n$ 是字符串的长度。空间复杂度 $O(1)$。
67+
6068
<!-- tabs:start -->
6169

6270
### **Python3**
6371

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

6674
```python
67-
75+
class Solution:
76+
def minimumSteps(self, s: str) -> int:
77+
n = len(s)
78+
ans = cnt = 0
79+
for i in range(n - 1, -1, -1):
80+
if s[i] == '1':
81+
cnt += 1
82+
ans += n - i - cnt
83+
return ans
6884
```
6985

7086
### **Java**
7187

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

7490
```java
75-
91+
class Solution {
92+
public long minimumSteps(String s) {
93+
long ans = 0;
94+
int cnt = 0;
95+
int n = s.length();
96+
for (int i = n - 1; i >= 0; --i) {
97+
if (s.charAt(i) == '1') {
98+
++cnt;
99+
ans += n - i - cnt;
100+
}
101+
}
102+
return ans;
103+
}
104+
}
76105
```
77106

78107
### **C++**
79108

80109
```cpp
81-
110+
class Solution {
111+
public:
112+
long long minimumSteps(string s) {
113+
long long ans = 0;
114+
int cnt = 0;
115+
int n = s.size();
116+
for (int i = n - 1; i >= 0; --i) {
117+
if (s[i] == '1') {
118+
++cnt;
119+
ans += n - i - cnt;
120+
}
121+
}
122+
return ans;
123+
}
124+
};
82125
```
83126
84127
### **Go**
85128
86129
```go
130+
func minimumSteps(s string) (ans int64) {
131+
n := len(s)
132+
cnt := 0
133+
for i := n - 1; i >= 0; i-- {
134+
if s[i] == '1' {
135+
cnt++
136+
ans += int64(n - i - cnt)
137+
}
138+
}
139+
return
140+
}
141+
```
87142

143+
### **TypeScript**
144+
145+
```ts
146+
function minimumSteps(s: string): number {
147+
const n = s.length;
148+
let [ans, cnt] = [0, 0];
149+
for (let i = n - 1; ~i; --i) {
150+
if (s[i] === '1') {
151+
++cnt;
152+
ans += n - i - cnt;
153+
}
154+
}
155+
return ans;
156+
}
88157
```
89158

90159
### **...**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /