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 e390190

Browse files
feat: add solutions to lc problem: No.1247.Minimum Swaps to Make Strings Equal (doocs#534)
1 parent 055d30c commit e390190

File tree

4 files changed

+119
-2
lines changed

4 files changed

+119
-2
lines changed

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

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,49 @@
7070

7171
### **Java**
7272

73-
<!-- 这里可写当前语言的特殊实现逻辑 -->
74-
7573
```java
74+
class Solution {
75+
public int minimumSwap(String s1, String s2) {
76+
int xy = 0, yx = 0;
77+
char[] c1 = s1.toCharArray();
78+
char[] c2 = s2.toCharArray();
79+
for (int i = 0; i < c1.length; i++) {
80+
if (c1[i] > c2[i]) {
81+
xy++;
82+
}
83+
if (c2[i] > c1[i]) {
84+
yx++;
85+
}
86+
}
87+
if ((xy + yx) % 2 != 0) {
88+
return -1;
89+
}
90+
return xy % 2 == 0 ? (xy + yx) / 2 : (xy + yx) / 2 + 1;
91+
}
92+
}
93+
```
7694

95+
### **Go**
96+
97+
```go
98+
func minimumSwap(s1 string, s2 string) int {
99+
xy, yx := 0, 0
100+
for i, _ := range s1 {
101+
if s1[i] < s2[i] {
102+
xy++
103+
}
104+
if s1[i] > s2[i] {
105+
yx++
106+
}
107+
}
108+
if (xy+yx)%2 != 0 {
109+
return -1
110+
}
111+
if xy%2 == 0 {
112+
return (xy + yx) / 2
113+
}
114+
return (xy+yx)/2 + 1
115+
}
77116
```
78117

79118
### **...**

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,48 @@ Note that you can&#39;t swap s1[0] and s1[1] to make s1 equal to &quot;yx&quot;,
102102
### **Java**
103103

104104
```java
105+
class Solution {
106+
public int minimumSwap(String s1, String s2) {
107+
int xy = 0, yx = 0;
108+
char[] c1 = s1.toCharArray();
109+
char[] c2 = s2.toCharArray();
110+
for (int i = 0; i < c1.length; i++) {
111+
if (c1[i] > c2[i]) {
112+
xy++;
113+
}
114+
if (c2[i] > c1[i]) {
115+
yx++;
116+
}
117+
}
118+
if ((xy + yx) % 2 != 0) {
119+
return -1;
120+
}
121+
return xy % 2 == 0 ? (xy + yx) / 2 : (xy + yx) / 2 + 1;
122+
}
123+
}
124+
```
105125

126+
### **Go**
127+
128+
```go
129+
func minimumSwap(s1 string, s2 string) int {
130+
xy, yx := 0, 0
131+
for i, _ := range s1 {
132+
if s1[i] < s2[i] {
133+
xy++
134+
}
135+
if s1[i] > s2[i] {
136+
yx++
137+
}
138+
}
139+
if (xy+yx)%2 != 0 {
140+
return -1
141+
}
142+
if xy%2 == 0 {
143+
return (xy + yx) / 2
144+
}
145+
return (xy+yx)/2 + 1
146+
}
106147
```
107148

108149
### **...**
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
func minimumSwap(s1 string, s2 string) int {
2+
xy, yx := 0, 0
3+
for i, _ := range s1 {
4+
if s1[i] < s2[i] {
5+
xy++
6+
}
7+
if s1[i] > s2[i] {
8+
yx++
9+
}
10+
}
11+
if (xy+yx)%2 != 0 {
12+
return -1
13+
}
14+
if xy%2 == 0 {
15+
return (xy + yx) / 2
16+
}
17+
return (xy+yx)/2 + 1
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int minimumSwap(String s1, String s2) {
3+
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++;
9+
}
10+
if (c2[i] > c1[i]) {
11+
yx++;
12+
}
13+
}
14+
if ((xy + yx) % 2 != 0) {
15+
return -1;
16+
}
17+
return xy % 2 == 0 ? (xy + yx) / 2 : (xy + yx) / 2 + 1;
18+
}
19+
}

0 commit comments

Comments
(0)

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