Skip to content

Commit ba11eef

Browse files
committed
Add solution and test-cases for problem 893
1 parent 1fbf15a commit ba11eef

File tree

3 files changed

+44
-23
lines changed

3 files changed

+44
-23
lines changed

leetcode/801-900/0893.Groups-of-Special-Equivalent-Strings/README.md

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
11
# [893.Groups of Special-Equivalent Strings][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 an array of strings of the same length `words`.
5+
6+
In one **move**, you can swap any two even indexed characters or any two odd indexed characters of a string `words[i]`.
7+
8+
Two strings `words[i]` and `words[j]` are **special-equivalent** if after any number of moves, `words[i] == words[j]`.
9+
10+
- For example, `words[i] = "zzxy"` and `words[j] = "xyzz"` are **special-equivalent** because we may make the moves `"zzxy" -> "xzzy" -> "xyzz"`.
11+
12+
A **group of special-equivalent strings** from `words` is a non-empty subset of words such that:
13+
14+
- Every pair of strings in the group are special equivalent, and
15+
- The group is the largest size possible (i.e., there is not a string `words[i]` not in the group such that `words[i]` is special-equivalent to every string in the group).
16+
17+
Return the number of **groups of special-equivalent strings** from `words`.
718

819
**Example 1:**
920

1021
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
22+
Input: words = ["abcd","cdab","cbad","xyzz","zzxy","zzyx"]
23+
Output: 3
24+
Explanation:
25+
One group is ["abcd", "cdab", "cbad"], since they are all pairwise special equivalent, and none of the other strings is all pairwise special equivalent to these.
26+
The other two groups are ["xyzz", "zzxy"] and ["zzyx"].
27+
Note that in particular, "zzxy" is not special equivalent to "zzyx".
1328
```
1429

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

20-
### 思路1
21-
> ...
22-
Groups of Special-Equivalent Strings
23-
```go
2432
```
25-
33+
Input: words = ["abc","acb","bac","bca","cab","cba"]
34+
Output: 3
35+
```
2636

2737
## 结语
2838

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(words []string) int {
4+
in := make(map[[2][26]int]int)
5+
for _, word := range words {
6+
k1, k2 := [26]int{}, [26]int{}
7+
for i := range word {
8+
if i&1 == 0 {
9+
k1[word[i]-'a']++
10+
continue
11+
}
12+
k2[word[i]-'a']++
13+
}
14+
in[[2][26]int{k1, k2}]++
15+
}
16+
return len(in)
517
}

leetcode/801-900/0893.Groups-of-Special-Equivalent-Strings/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []string
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []string{"abcd", "cdab", "cbad", "xyzz", "zzxy", "zzyx"}, 3},
17+
{"TestCase2", []string{"abc", "acb", "bac", "bca", "cab", "cba"}, 3},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)