Movatterモバイル変換


[0]ホーム

URL:


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

Commit5420eb3

Browse files
committed
Add solution and test-cases for problem 1247
1 parentd6fa07d commit5420eb3

File tree

3 files changed

+55
-25
lines changed

3 files changed

+55
-25
lines changed

‎leetcode/1201-1300/1247.Minimum-Swaps-to-Make-Strings-Equal/README.md

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
#[1247.Minimum Swaps to Make Strings Equal][title]
22

3-
>[!WARNING|style:flat]
4-
>This question is temporarily unanswered if you have good ideas. Welcome to[Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
##Description
4+
You are given two strings`s1` and`s2` of equal length consisting of letters`"x"` and`"y"`**only**. Your task is to make these two strings equal to each other. You can swap any two characters that belong to**different** strings, which means: swap`s1[i]` and`s2[i]`.
5+
6+
Return the minimum number of swaps required to make`s1` and`s2` equal, or return`-1` if it is impossible to do so.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: s1 = "xx", s2 = "yy"
12+
Output: 1
13+
Explanation: Swap s1[0] and s2[1], s1 = "yx", s2 = "yx".
1314
```
1415

15-
##题意
16-
>...
16+
**Example 2:**
1717

18-
##题解
19-
20-
###思路1
21-
>...
22-
Minimum Swaps to Make Strings Equal
23-
```go
18+
```
19+
Input: s1 = "xy", s2 = "yx"
20+
Output: 2
21+
Explanation: Swap s1[0] and s2[0], s1 = "yy", s2 = "xx".
22+
Swap s1[0] and s2[1], s1 = "xy", s2 = "xy".
23+
Note that you cannot swap s1[0] and s1[1] to make s1 equal to
2424
```
2525

26+
**Example 3:**
27+
28+
```
29+
Input: s1 = "xx", s2 = "xy"
30+
Output: -1
31+
```
2632

2733
##结语
2834

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
package Solution
22

3-
funcSolution(xbool)bool {
4-
returnx
3+
funcSolution(s1string,s2string)int {
4+
// 寻找diff的情况
5+
iflen(s1)!=len(s2) {
6+
return-1
7+
}
8+
zero,one:=0,0
9+
fori:=0;i<len(s1);i++ {
10+
ifs1[i]!=s2[i] {
11+
ifs1[i]=='x' {
12+
// xy
13+
zero++
14+
}else {
15+
// yx
16+
one++
17+
}
18+
}
19+
}
20+
if (one+zero)&1==1 {
21+
return-1
22+
}
23+
ans:=one/2
24+
ans+=zero/2
25+
ifone&1==1 {
26+
ans+=2
27+
}
28+
returnans
529
}

‎leetcode/1201-1300/1247.Minimum-Swaps-to-Make-Strings-Equal/Solution_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
1010
//测试用例
1111
cases:= []struct {
1212
namestring
13-
inputsbool
14-
expectbool
13+
s1,s2string
14+
expectint
1515
}{
16-
{"TestCase",true,true},
17-
{"TestCase",true,true},
18-
{"TestCase",false,false},
16+
{"TestCase1","xx","yy",1},
17+
{"TestCase2","xy","yx",2},
18+
{"TestCase3","xx","xy",-1},
1919
}
2020

2121
//开始测试
2222
fori,c:=rangecases {
2323
t.Run(c.name+" "+strconv.Itoa(i),func(t*testing.T) {
24-
got:=Solution(c.inputs)
24+
got:=Solution(c.s1,c.s2)
2525
if!reflect.DeepEqual(got,c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect,got,c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect,got,c.s1,c.s2)
2828
}
2929
})
3030
}
3131
}
3232

33-
//压力测试
33+
//压力测试
3434
funcBenchmarkSolution(b*testing.B) {
3535
}
3636

37-
//使用案列
37+
//使用案列
3838
funcExampleSolution() {
3939
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp