Skip to content

feat: add solutions to lc problem: No.1247.Minimum Swaps to Make Strings Equal #534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,49 @@

### **Java**

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

```java
class Solution {
public int minimumSwap(String s1, String s2) {
int xy = 0, yx = 0;
char[] c1 = s1.toCharArray();
char[] c2 = s2.toCharArray();
for (int i = 0; i < c1.length; i++) {
if (c1[i] > c2[i]) {
xy++;
}
if (c2[i] > c1[i]) {
yx++;
}
}
if ((xy + yx) % 2 != 0) {
return -1;
}
return xy % 2 == 0 ? (xy + yx) / 2 : (xy + yx) / 2 + 1;
}
}
```

### **Go**

```go
func minimumSwap(s1 string, s2 string) int {
xy, yx := 0, 0
for i, _ := range s1 {
if s1[i] < s2[i] {
xy++
}
if s1[i] > s2[i] {
yx++
}
}
if (xy+yx)%2 != 0 {
return -1
}
if xy%2 == 0 {
return (xy + yx) / 2
}
return (xy+yx)/2 + 1
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,48 @@ Note that you can&#39;t swap s1[0] and s1[1] to make s1 equal to &quot;yx&quot;,
### **Java**

```java
class Solution {
public int minimumSwap(String s1, String s2) {
int xy = 0, yx = 0;
char[] c1 = s1.toCharArray();
char[] c2 = s2.toCharArray();
for (int i = 0; i < c1.length; i++) {
if (c1[i] > c2[i]) {
xy++;
}
if (c2[i] > c1[i]) {
yx++;
}
}
if ((xy + yx) % 2 != 0) {
return -1;
}
return xy % 2 == 0 ? (xy + yx) / 2 : (xy + yx) / 2 + 1;
}
}
```

### **Go**

```go
func minimumSwap(s1 string, s2 string) int {
xy, yx := 0, 0
for i, _ := range s1 {
if s1[i] < s2[i] {
xy++
}
if s1[i] > s2[i] {
yx++
}
}
if (xy+yx)%2 != 0 {
return -1
}
if xy%2 == 0 {
return (xy + yx) / 2
}
return (xy+yx)/2 + 1
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
func minimumSwap(s1 string, s2 string) int {
xy, yx := 0, 0
for i, _ := range s1 {
if s1[i] < s2[i] {
xy++
}
if s1[i] > s2[i] {
yx++
}
}
if (xy+yx)%2 != 0 {
return -1
}
if xy%2 == 0 {
return (xy + yx) / 2
}
return (xy+yx)/2 + 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public int minimumSwap(String s1, String s2) {
int xy = 0, yx = 0;
char[] c1 = s1.toCharArray();
char[] c2 = s2.toCharArray();
for (int i = 0; i < c1.length; i++) {
if (c1[i] > c2[i]) {
xy++;
}
if (c2[i] > c1[i]) {
yx++;
}
}
if ((xy + yx) % 2 != 0) {
return -1;
}
return xy % 2 == 0 ? (xy + yx) / 2 : (xy + yx) / 2 + 1;
}
}