Skip to content

Commit db4a6f3

Browse files
authored
feat: add solutions to lc problem: No.1897 (#3858)
No.1897.Redistribute Characters to Make All Strings Equal
1 parent 529aa83 commit db4a6f3

File tree

8 files changed

+156
-104
lines changed

8 files changed

+156
-104
lines changed

solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/README.md

+54-33
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,13 @@ tags:
6060

6161
<!-- solution:start -->
6262

63-
### 方法一
63+
### 方法一:计数
64+
65+
根据题目描述,只要每个字符的出现次数能被字符串数组的长度整除,就可以通过移动字符使所有字符串相等。
66+
67+
因此,我们用哈希表或者一个长度为 $26$ 的整数数组 $\textit{cnt}$ 统计每个字符出现的次数,最后判断是否每个字符的出现次数能被字符串数组的长度整除即可。
68+
69+
时间复杂度 $O(L)$,空间复杂度 $O(|\Sigma|)$。其中 $L$ 为数组 $\textit{words}$ 中所有字符串的长度之和,而 $\Sigma$ 为字符集,这里为小写字母集合,所以 $|\Sigma|=26$。
6470

6571
<!-- tabs:start -->
6672

@@ -69,28 +75,28 @@ tags:
6975
```python
7076
class Solution:
7177
def makeEqual(self, words: List[str]) -> bool:
72-
counter = Counter()
73-
for word in words:
74-
for c in word:
75-
counter[c] += 1
78+
cnt = Counter()
79+
for w in words:
80+
for c in w:
81+
cnt[c] += 1
7682
n = len(words)
77-
return all(count % n == 0 for count in counter.values())
83+
return all(v % n == 0 for v in cnt.values())
7884
```
7985

8086
#### Java
8187

8288
```java
8389
class Solution {
8490
public boolean makeEqual(String[] words) {
85-
int[] counter = new int[26];
86-
for (String word : words) {
87-
for (char c : word.toCharArray()) {
88-
++counter[c - 'a'];
91+
int[] cnt = new int[26];
92+
for (var w : words) {
93+
for (char c : w.toCharArray()) {
94+
++cnt[c - 'a'];
8995
}
9096
}
9197
int n = words.length;
92-
for (int i = 0; i < 26; ++i) {
93-
if (counter[i] % n != 0) {
98+
for (int v : cnt) {
99+
if (v % n != 0) {
94100
return false;
95101
}
96102
}
@@ -105,15 +111,17 @@ class Solution {
105111
class Solution {
106112
public:
107113
bool makeEqual(vector<string>& words) {
108-
vector<int> counter(26, 0);
109-
for (string word : words) {
110-
for (char c : word) {
111-
++counter[c - 'a'];
114+
int cnt[26]{};
115+
for (const auto& w : words) {
116+
for (const auto& c : w) {
117+
++cnt[c - 'a'];
112118
}
113119
}
114120
int n = words.size();
115-
for (int count : counter) {
116-
if (count % n != 0) return false;
121+
for (int i = 0; i < 26; ++i) {
122+
if (cnt[i] % n != 0) {
123+
return false;
124+
}
117125
}
118126
return true;
119127
}
@@ -124,15 +132,15 @@ public:
124132
125133
```go
126134
func makeEqual(words []string) bool {
127-
counter := [26]int{}
128-
for _, word := range words {
129-
for _, c := range word {
130-
counter[c-'a']++
135+
cnt := [26]int{}
136+
for _, w := range words {
137+
for _, c := range w {
138+
cnt[c-'a']++
131139
}
132140
}
133141
n := len(words)
134-
for _, count := range counter {
135-
if count%n != 0 {
142+
for _, v := range cnt {
143+
if v%n != 0 {
136144
return false
137145
}
138146
}
@@ -144,20 +152,33 @@ func makeEqual(words []string) bool {
144152

145153
```ts
146154
function makeEqual(words: string[]): boolean {
147-
let n = words.length;
148-
let letters = new Array(26).fill(0);
149-
for (let word of words) {
150-
for (let i = 0; i < word.length; ++i) {
151-
++letters[word.charCodeAt(i) - 97];
155+
const cnt: Record<string, number> = {};
156+
for (const w of words) {
157+
for (const c of w) {
158+
cnt[c] = (cnt[c] || 0) + 1;
152159
}
153160
}
161+
const n = words.length;
162+
return Object.values(cnt).every(v => v % n === 0);
163+
}
164+
```
165+
166+
#### Rust
154167

155-
for (let i = 0; i < letters.length; ++i) {
156-
if (letters[i] % n != 0) {
157-
return false;
168+
```rust
169+
impl Solution {
170+
pub fn make_equal(words: Vec<String>) -> bool {
171+
let mut cnt = std::collections::HashMap::new();
172+
173+
for word in words.iter() {
174+
for c in word.chars() {
175+
*cnt.entry(c).or_insert(0) += 1;
176+
}
158177
}
178+
179+
let n = words.len();
180+
cnt.values().all(|&v| v % n == 0)
159181
}
160-
return true;
161182
}
162183
```
163184

solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/README_EN.md

+54-33
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,13 @@ All the strings are now equal to &quot;abc&quot;, so return <code>true</code>.
6060

6161
<!-- solution:start -->
6262

63-
### Solution 1
63+
### Solution 1: Counting
64+
65+
According to the problem description, as long as the occurrence count of each character can be divided by the length of the string array, it is possible to redistribute the characters to make all strings equal.
66+
67+
Therefore, we use a hash table or an integer array of length $26$ $\textit{cnt}$ to count the occurrences of each character. Finally, we check if the occurrence count of each character can be divided by the length of the string array.
68+
69+
The time complexity is $O(L)$, and the space complexity is $O(|\Sigma|)$. Here, $L$ is the total length of all strings in the array $\textit{words}$, and $\Sigma$ is the character set, which is the set of lowercase letters, so $|\Sigma|=26$.
6470

6571
<!-- tabs:start -->
6672

@@ -69,28 +75,28 @@ All the strings are now equal to &quot;abc&quot;, so return <code>true</code>.
6975
```python
7076
class Solution:
7177
def makeEqual(self, words: List[str]) -> bool:
72-
counter = Counter()
73-
for word in words:
74-
for c in word:
75-
counter[c] += 1
78+
cnt = Counter()
79+
for w in words:
80+
for c in w:
81+
cnt[c] += 1
7682
n = len(words)
77-
return all(count % n == 0 for count in counter.values())
83+
return all(v % n == 0 for v in cnt.values())
7884
```
7985

8086
#### Java
8187

8288
```java
8389
class Solution {
8490
public boolean makeEqual(String[] words) {
85-
int[] counter = new int[26];
86-
for (String word : words) {
87-
for (char c : word.toCharArray()) {
88-
++counter[c - 'a'];
91+
int[] cnt = new int[26];
92+
for (var w : words) {
93+
for (char c : w.toCharArray()) {
94+
++cnt[c - 'a'];
8995
}
9096
}
9197
int n = words.length;
92-
for (int i = 0; i < 26; ++i) {
93-
if (counter[i] % n != 0) {
98+
for (int v : cnt) {
99+
if (v % n != 0) {
94100
return false;
95101
}
96102
}
@@ -105,15 +111,17 @@ class Solution {
105111
class Solution {
106112
public:
107113
bool makeEqual(vector<string>& words) {
108-
vector<int> counter(26, 0);
109-
for (string word : words) {
110-
for (char c : word) {
111-
++counter[c - 'a'];
114+
int cnt[26]{};
115+
for (const auto& w : words) {
116+
for (const auto& c : w) {
117+
++cnt[c - 'a'];
112118
}
113119
}
114120
int n = words.size();
115-
for (int count : counter) {
116-
if (count % n != 0) return false;
121+
for (int i = 0; i < 26; ++i) {
122+
if (cnt[i] % n != 0) {
123+
return false;
124+
}
117125
}
118126
return true;
119127
}
@@ -124,15 +132,15 @@ public:
124132
125133
```go
126134
func makeEqual(words []string) bool {
127-
counter := [26]int{}
128-
for _, word := range words {
129-
for _, c := range word {
130-
counter[c-'a']++
135+
cnt := [26]int{}
136+
for _, w := range words {
137+
for _, c := range w {
138+
cnt[c-'a']++
131139
}
132140
}
133141
n := len(words)
134-
for _, count := range counter {
135-
if count%n != 0 {
142+
for _, v := range cnt {
143+
if v%n != 0 {
136144
return false
137145
}
138146
}
@@ -144,20 +152,33 @@ func makeEqual(words []string) bool {
144152

145153
```ts
146154
function makeEqual(words: string[]): boolean {
147-
let n = words.length;
148-
let letters = new Array(26).fill(0);
149-
for (let word of words) {
150-
for (let i = 0; i < word.length; ++i) {
151-
++letters[word.charCodeAt(i) - 97];
155+
const cnt: Record<string, number> = {};
156+
for (const w of words) {
157+
for (const c of w) {
158+
cnt[c] = (cnt[c] || 0) + 1;
152159
}
153160
}
161+
const n = words.length;
162+
return Object.values(cnt).every(v => v % n === 0);
163+
}
164+
```
165+
166+
#### Rust
154167

155-
for (let i = 0; i < letters.length; ++i) {
156-
if (letters[i] % n != 0) {
157-
return false;
168+
```rust
169+
impl Solution {
170+
pub fn make_equal(words: Vec<String>) -> bool {
171+
let mut cnt = std::collections::HashMap::new();
172+
173+
for word in words.iter() {
174+
for c in word.chars() {
175+
*cnt.entry(c).or_insert(0) += 1;
176+
}
158177
}
178+
179+
let n = words.len();
180+
cnt.values().all(|&v| v % n == 0)
159181
}
160-
return true;
161182
}
162183
```
163184

Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
class Solution {
22
public:
33
bool makeEqual(vector<string>& words) {
4-
vector<int> counter(26, 0);
5-
for (string word : words) {
6-
for (char c : word) {
7-
++counter[c - 'a'];
4+
int cnt[26]{};
5+
for (const auto& w : words) {
6+
for (const auto& c : w) {
7+
++cnt[c - 'a'];
88
}
99
}
1010
int n = words.size();
11-
for (int count : counter) {
12-
if (count % n != 0) return false;
11+
for (int i = 0; i < 26; ++i) {
12+
if (cnt[i] % n != 0) {
13+
return false;
14+
}
1315
}
1416
return true;
1517
}
16-
};
18+
};
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
func makeEqual(words []string) bool {
2-
counter := [26]int{}
3-
for _, word := range words {
4-
for _, c := range word {
5-
counter[c-'a']++
2+
cnt := [26]int{}
3+
for _, w := range words {
4+
for _, c := range w {
5+
cnt[c-'a']++
66
}
77
}
88
n := len(words)
9-
for _, count := range counter {
10-
if count%n != 0 {
9+
for _, v := range cnt {
10+
if v%n != 0 {
1111
return false
1212
}
1313
}
1414
return true
15-
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
class Solution {
22
public boolean makeEqual(String[] words) {
3-
int[] counter = new int[26];
4-
for (String word : words) {
5-
for (char c : word.toCharArray()) {
6-
++counter[c - 'a'];
3+
int[] cnt = new int[26];
4+
for (var w : words) {
5+
for (char c : w.toCharArray()) {
6+
++cnt[c - 'a'];
77
}
88
}
99
int n = words.length;
10-
for (int i = 0; i < 26; ++i) {
11-
if (counter[i] % n != 0) {
10+
for (int v : cnt) {
11+
if (v % n != 0) {
1212
return false;
1313
}
1414
}
1515
return true;
1616
}
17-
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
class Solution:
22
def makeEqual(self, words: List[str]) -> bool:
3-
counter = Counter()
4-
for word in words:
5-
for c in word:
6-
counter[c] += 1
3+
cnt = Counter()
4+
for w in words:
5+
for c in w:
6+
cnt[c] += 1
77
n = len(words)
8-
return all(count % n == 0 for count in counter.values())
8+
return all(v % n == 0 for v in cnt.values())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
impl Solution {
2+
pub fn make_equal(words: Vec<String>) -> bool {
3+
let mut cnt = std::collections::HashMap::new();
4+
5+
for word in words.iter() {
6+
for c in word.chars() {
7+
*cnt.entry(c).or_insert(0) += 1;
8+
}
9+
}
10+
11+
let n = words.len();
12+
cnt.values().all(|&v| v % n == 0)
13+
}
14+
}

0 commit comments

Comments
 (0)