Skip to content

Commit a0bb634

Browse files
authored
Merge pull request 6boris#726 from 0xff-dev/1897
Add solution and test-cases for problem 1897
2 parents 2d2af47 + 2d039d7 commit a0bb634

File tree

3 files changed

+34
-22
lines changed

3 files changed

+34
-22
lines changed

leetcode/1801-1900/1897.Redistribute-Characters-to-Make-All-Strings-Equal/README.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
# [1897.Redistribute Characters to Make All 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 an array of strings `words` (0-indexed).
5+
6+
In one operation, pick two **distinct** indices `i` and `j`, where `words[i]` is a non-empty string, and move **any** character from `words[i]` to any position in `words[j]`.
7+
8+
Return `true` if you can make **every** string in `wrods` **equal** using **any** number of operations, and `false` otherwise.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: words = ["abc","aabc","bc"]
14+
Output: true
15+
Explanation: Move the first 'a' in words[1] to the front of words[2],
16+
to make words[1] = "abc" and words[2] = "abc".
17+
All the strings are now equal to "abc", so return true.
1318
```
1419

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

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Redistribute Characters to Make All Strings Equal
23-
```go
2422
```
25-
23+
Input: words = ["ab","a"]
24+
Output: false
25+
Explanation: It is impossible to make all the strings equal using the operation.
26+
```
2627

2728
## 结语
2829

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) bool {
4+
letters := [26]int{}
5+
for _, word := range words {
6+
for _, b := range word {
7+
letters[b-'a']++
8+
}
9+
}
10+
l := len(words)
11+
for i := 0; i < 26; i++ {
12+
if letters[i]%l != 0 {
13+
return false
14+
}
15+
}
16+
return true
517
}

leetcode/1801-1900/1897.Redistribute-Characters-to-Make-All-Strings-Equal/Solution_test.go

Lines changed: 5 additions & 6 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
13+
inputs []string
1414
expect bool
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []string{"abc", "aabc", "bc"}, true},
17+
{"TestCase2", []string{"ab", "a"}, false},
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)