Skip to content

Commit 1c8ae4c

Browse files
committed
feat: add solutions to lc problem: No.1247
No.1247.Minimum Swaps to Make Strings Equal
1 parent 9da49d1 commit 1c8ae4c

File tree

6 files changed

+125
-47
lines changed

6 files changed

+125
-47
lines changed

solution/1200-1299/1247.Minimum Swaps to Make Strings Equal/README.md

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,32 @@
5757

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

60+
**方法一:贪心**
61+
62+
根据题目描述,两个字符串 $s1$ 和 $s2$ 都只包含字符 `'x'``'y'`,且长度相同,因此可以将 $s1$ 和 $s2$ 中的字符一一对应起来,即 $s1[i]$ 和 $s2[i]$。
63+
64+
如果 $s1[i] = s2[i]$,则不需要交换,直接跳过即可。如果 $s1[i] \neq s2[i]$,则需要交换,我们统计 $s1[i]$ 和 $s2[i]$ 的组合情况,即 $s1[i] = 'x'$ 且 $s2[i] = 'y'$ 的情况,记为 $xy$,而 $s1[i] = 'y'$ 且 $s2[i] = 'x'$ 的情况,记为 $yx$。
65+
66+
如果 $xy + yx$ 为奇数,则无法完成交换,返回 $-1$。如果 $xy + yx$ 为偶数,则需要交换的次数为 $\left \lfloor \frac{x}{2} \right \rfloor$ + $\left \lfloor \frac{y}{2} \right \rfloor$ + $xy \mod{2}$ + $yx \mod{2}$。
67+
68+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $s1$ 和 $s2$ 的长度。
69+
6070
<!-- tabs:start -->
6171

6272
### **Python3**
6373

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

6676
```python
67-
77+
class Solution:
78+
def minimumSwap(self, s1: str, s2: str) -> int:
79+
xy = yx = 0
80+
for a, b in zip(s1, s2):
81+
xy += a < b
82+
yx += a > b
83+
if (xy + yx) % 2:
84+
return -1
85+
return xy // 2 + yx // 2 + xy % 2 + yx % 2
6886
```
6987

7088
### **Java**
@@ -73,44 +91,60 @@
7391
class Solution {
7492
public int minimumSwap(String s1, String s2) {
7593
int xy = 0, yx = 0;
76-
char[] c1 = s1.toCharArray();
77-
char[] c2 = s2.toCharArray();
78-
for (int i = 0; i < c1.length; i++) {
79-
if (c1[i] > c2[i]) {
80-
xy++;
94+
for (int i = 0; i < s1.length(); ++i) {
95+
char a = s1.charAt(i), b = s2.charAt(i);
96+
if (a < b) {
97+
++xy;
8198
}
82-
if (c2[i] > c1[i]) {
83-
yx++;
99+
if (a > b) {
100+
++yx;
84101
}
85102
}
86-
if ((xy + yx) % 2 != 0) {
103+
if ((xy + yx) % 2 == 1) {
87104
return -1;
88105
}
89-
return xy % 2 == 0 ? (xy + yx) / 2 : (xy + yx) / 2 + 1;
106+
return xy / 2 + yx / 2 + xy % 2 + yx % 2;
90107
}
91108
}
92109
```
93110

111+
### **C++**
112+
113+
```cpp
114+
class Solution {
115+
public:
116+
int minimumSwap(string s1, string s2) {
117+
int xy = 0, yx = 0;
118+
for (int i = 0; i < s1.size(); ++i) {
119+
char a = s1[i], b = s2[i];
120+
xy += a < b;
121+
yx += a > b;
122+
}
123+
if ((xy + yx) % 2) {
124+
return -1;
125+
}
126+
return xy / 2 + yx / 2 + xy % 2 + yx % 2;
127+
}
128+
};
129+
```
130+
94131
### **Go**
95132
96133
```go
97134
func minimumSwap(s1 string, s2 string) int {
98135
xy, yx := 0, 0
99-
for i, _ := range s1 {
136+
for i := range s1 {
100137
if s1[i] < s2[i] {
101138
xy++
102139
}
103140
if s1[i] > s2[i] {
104141
yx++
105142
}
106143
}
107-
if (xy+yx)%2 != 0 {
144+
if (xy+yx)%2 == 1 {
108145
return -1
109146
}
110-
if xy%2 == 0 {
111-
return (xy + yx) / 2
112-
}
113-
return (xy+yx)/2 + 1
147+
return xy/2 + yx/2 + xy%2 + yx%2
114148
}
115149
```
116150

solution/1200-1299/1247.Minimum Swaps to Make Strings Equal/README_EN.md

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,15 @@ Note that you cannot swap s1[0] and s1[1] to make s1 equal to &quot;yx&quot;, ca
4949
### **Python3**
5050

5151
```python
52-
52+
class Solution:
53+
def minimumSwap(self, s1: str, s2: str) -> int:
54+
xy = yx = 0
55+
for a, b in zip(s1, s2):
56+
xy += a < b
57+
yx += a > b
58+
if (xy + yx) % 2:
59+
return -1
60+
return xy // 2 + yx // 2 + xy % 2 + yx % 2
5361
```
5462

5563
### **Java**
@@ -58,44 +66,60 @@ Note that you cannot swap s1[0] and s1[1] to make s1 equal to &quot;yx&quot;, ca
5866
class Solution {
5967
public int minimumSwap(String s1, String s2) {
6068
int xy = 0, yx = 0;
61-
char[] c1 = s1.toCharArray();
62-
char[] c2 = s2.toCharArray();
63-
for (int i = 0; i < c1.length; i++) {
64-
if (c1[i] > c2[i]) {
65-
xy++;
69+
for (int i = 0; i < s1.length(); ++i) {
70+
char a = s1.charAt(i), b = s2.charAt(i);
71+
if (a < b) {
72+
++xy;
6673
}
67-
if (c2[i] > c1[i]) {
68-
yx++;
74+
if (a > b) {
75+
++yx;
6976
}
7077
}
71-
if ((xy + yx) % 2 != 0) {
78+
if ((xy + yx) % 2 == 1) {
7279
return -1;
7380
}
74-
return xy % 2 == 0 ? (xy + yx) / 2 : (xy + yx) / 2 + 1;
81+
return xy / 2 + yx / 2 + xy % 2 + yx % 2;
7582
}
7683
}
7784
```
7885

86+
### **C++**
87+
88+
```cpp
89+
class Solution {
90+
public:
91+
int minimumSwap(string s1, string s2) {
92+
int xy = 0, yx = 0;
93+
for (int i = 0; i < s1.size(); ++i) {
94+
char a = s1[i], b = s2[i];
95+
xy += a < b;
96+
yx += a > b;
97+
}
98+
if ((xy + yx) % 2) {
99+
return -1;
100+
}
101+
return xy / 2 + yx / 2 + xy % 2 + yx % 2;
102+
}
103+
};
104+
```
105+
79106
### **Go**
80107
81108
```go
82109
func minimumSwap(s1 string, s2 string) int {
83110
xy, yx := 0, 0
84-
for i, _ := range s1 {
111+
for i := range s1 {
85112
if s1[i] < s2[i] {
86113
xy++
87114
}
88115
if s1[i] > s2[i] {
89116
yx++
90117
}
91118
}
92-
if (xy+yx)%2 != 0 {
119+
if (xy+yx)%2 == 1 {
93120
return -1
94121
}
95-
if xy%2 == 0 {
96-
return (xy + yx) / 2
97-
}
98-
return (xy+yx)/2 + 1
122+
return xy/2 + yx/2 + xy%2 + yx%2
99123
}
100124
```
101125

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int minimumSwap(string s1, string s2) {
4+
int xy = 0, yx = 0;
5+
for (int i = 0; i < s1.size(); ++i) {
6+
char a = s1[i], b = s2[i];
7+
xy += a < b;
8+
yx += a > b;
9+
}
10+
if ((xy + yx) % 2) {
11+
return -1;
12+
}
13+
return xy / 2 + yx / 2 + xy % 2 + yx % 2;
14+
}
15+
};
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
func minimumSwap(s1 string, s2 string) int {
22
xy, yx := 0, 0
3-
for i, _ := range s1 {
3+
for i := range s1 {
44
if s1[i] < s2[i] {
55
xy++
66
}
77
if s1[i] > s2[i] {
88
yx++
99
}
1010
}
11-
if (xy+yx)%2 != 0 {
11+
if (xy+yx)%2 == 1 {
1212
return -1
1313
}
14-
if xy%2 == 0 {
15-
return (xy + yx) / 2
16-
}
17-
return (xy+yx)/2 + 1
14+
return xy/2 + yx/2 + xy%2 + yx%2
1815
}
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
class Solution {
22
public int minimumSwap(String s1, String s2) {
33
int xy = 0, yx = 0;
4-
char[] c1 = s1.toCharArray();
5-
char[] c2 = s2.toCharArray();
6-
for (int i = 0; i < c1.length; i++) {
7-
if (c1[i] > c2[i]) {
8-
xy++;
4+
for (int i = 0; i < s1.length(); ++i) {
5+
char a = s1.charAt(i), b = s2.charAt(i);
6+
if (a < b) {
7+
++xy;
98
}
10-
if (c2[i] > c1[i]) {
11-
yx++;
9+
if (a > b) {
10+
++yx;
1211
}
1312
}
14-
if ((xy + yx) % 2 != 0) {
13+
if ((xy + yx) % 2 == 1) {
1514
return -1;
1615
}
17-
return xy % 2 == 0 ? (xy + yx) / 2 : (xy + yx) / 2 + 1;
16+
return xy / 2 + yx / 2 + xy % 2 + yx % 2;
1817
}
1918
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def minimumSwap(self, s1: str, s2: str) -> int:
3+
xy = yx = 0
4+
for a, b in zip(s1, s2):
5+
xy += a < b
6+
yx += a > b
7+
if (xy + yx) % 2:
8+
return -1
9+
return xy // 2 + yx // 2 + xy % 2 + yx % 2

0 commit comments

Comments
 (0)