Skip to content

Commit a23fed4

Browse files
authored
Merge pull request 6boris#361 from 0xff-dev/1657
Add solution and test-cases for problem 1657
2 parents 4e0bc23 + 134023f commit a23fed4

File tree

3 files changed

+66
-24
lines changed

3 files changed

+66
-24
lines changed

leetcode/1601-1700/1657.Determine-if-Two-Strings-Are-Close/README.md

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,46 @@
11
# [1657.Determine if Two Strings Are Close][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+
Two strings are considered **close** if you can attain one from the other using the following operations:
5+
6+
- Operation 1: Swap any two **existing** characters.
7+
- For example, `abcde -> aecdb`
8+
9+
- Operation 2: Transform **every** occurrence of one **existing** character into another **existing** character, and do the same with the other character.
10+
- For example, `aacabb -> bbcbaa` (all `a`'s turn into `b`'s, and all `b`'s turn into `a`'s)
11+
You can use the operations on either string as many times as necessary.
712

13+
Given two strings, `word1` and `word2`, return `true` if `word1` and `word2` are **close**, and `false` otherwise.
14+
15+
816
**Example 1:**
917

1018
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
19+
Input: word1 = "abc", word2 = "bca"
20+
Output: true
21+
Explanation: You can attain word2 from word1 in 2 operations.
22+
Apply Operation 1: "abc" -> "acb"
23+
Apply Operation 1: "acb" -> "bca"
1324
```
1425

15-
## 题意
16-
> ...
17-
18-
## 题解
26+
**Example 2:**
1927

20-
### 思路1
21-
> ...
22-
Determine if Two Strings Are Close
23-
```go
2428
```
29+
Input: word1 = "a", word2 = "aa"
30+
Output: false
31+
Explanation: It is impossible to attain word2 from word1, or vice versa, in any number of operations.
32+
```
33+
34+
**Example 3:**
2535

36+
```
37+
Input: word1 = "cabbba", word2 = "abbccc"
38+
Output: true
39+
Explanation: You can attain word2 from word1 in 3 operations.
40+
Apply Operation 1: "cabbba" -> "caabbb"
41+
Apply Operation 2: "caabbb" -> "baaccc"
42+
Apply Operation 2: "baaccc" -> "abbccc"
43+
```
2644

2745
## 结语
2846

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

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(word1 string, word2 string) bool {
6+
if len(word1) != len(word2) {
7+
return false
8+
}
9+
bucket1, bucket2 := make([]int, 26), make([]int, 26)
10+
for idx := 0; idx < len(word1); idx++ {
11+
bucket1[word1[idx]-'a']++
12+
bucket2[word2[idx]-'a']++
13+
}
14+
15+
for i := 0; i < 26; i++ {
16+
if bucket2[i] != 0 && bucket1[i] == 0 || bucket1[i] != 0 && bucket2[i] == 0 {
17+
return false
18+
}
19+
}
20+
sort.Ints(bucket1)
21+
sort.Ints(bucket2)
22+
23+
for i := 0; i < 26; i++ {
24+
if bucket1[i] != bucket2[i] {
25+
return false
26+
}
27+
}
28+
return true
529
}

leetcode/1601-1700/1657.Determine-if-Two-Strings-Are-Close/Solution_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
13+
s1, s2 string
1414
expect bool
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "abc", "bca", true},
17+
{"TestCase2", "a", "aa", false},
18+
{"TestCase3", "cabbba", "abbccc", true},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
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
func BenchmarkSolution(b *testing.B) {
3535
}
3636

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

0 commit comments

Comments
 (0)