From 5b44761954686cb53430b36b3cb02fde3631a1ae Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sat, 14 Dec 2024 08:39:19 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.1897 No.1897.Redistribute Characters to Make All Strings Equal --- .../README.md | 87 ++++++++++++------- .../README_EN.md | 87 ++++++++++++------- .../Solution.cpp | 16 ++-- .../Solution.go | 14 +-- .../Solution.java | 14 +-- .../Solution.py | 10 +-- .../Solution.rs | 14 +++ .../Solution.ts | 18 ++-- 8 files changed, 156 insertions(+), 104 deletions(-) create mode 100644 solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/Solution.rs diff --git a/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/README.md b/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/README.md index 07ac29f00b24c..63b3a42104079 100644 --- a/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/README.md +++ b/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/README.md @@ -60,7 +60,13 @@ tags: -### 方法一 +### 方法一:计数 + +根据题目描述,只要每个字符的出现次数能被字符串数组的长度整除,就可以通过移动字符使所有字符串相等。 + +因此,我们用哈希表或者一个长度为 $26$ 的整数数组 $\textit{cnt}$ 统计每个字符出现的次数,最后判断是否每个字符的出现次数能被字符串数组的长度整除即可。 + +时间复杂度 $O(L)$,空间复杂度 $O(|\Sigma|)$。其中 $L$ 为数组 $\textit{words}$ 中所有字符串的长度之和,而 $\Sigma$ 为字符集,这里为小写字母集合,所以 $|\Sigma|=26$。 @@ -69,12 +75,12 @@ tags: ```python class Solution: def makeEqual(self, words: List[str]) -> bool: - counter = Counter() - for word in words: - for c in word: - counter[c] += 1 + cnt = Counter() + for w in words: + for c in w: + cnt[c] += 1 n = len(words) - return all(count % n == 0 for count in counter.values()) + return all(v % n == 0 for v in cnt.values()) ``` #### Java @@ -82,15 +88,15 @@ class Solution: ```java class Solution { public boolean makeEqual(String[] words) { - int[] counter = new int[26]; - for (String word : words) { - for (char c : word.toCharArray()) { - ++counter[c - 'a']; + int[] cnt = new int[26]; + for (var w : words) { + for (char c : w.toCharArray()) { + ++cnt[c - 'a']; } } int n = words.length; - for (int i = 0; i < 26; ++i) { - if (counter[i] % n != 0) { + for (int v : cnt) { + if (v % n != 0) { return false; } } @@ -105,15 +111,17 @@ class Solution { class Solution { public: bool makeEqual(vector& words) { - vector counter(26, 0); - for (string word : words) { - for (char c : word) { - ++counter[c - 'a']; + int cnt[26]{}; + for (const auto& w : words) { + for (const auto& c : w) { + ++cnt[c - 'a']; } } int n = words.size(); - for (int count : counter) { - if (count % n != 0) return false; + for (int i = 0; i < 26; ++i) { + if (cnt[i] % n != 0) { + return false; + } } return true; } @@ -124,15 +132,15 @@ public: ```go func makeEqual(words []string) bool { - counter := [26]int{} - for _, word := range words { - for _, c := range word { - counter[c-'a']++ + cnt := [26]int{} + for _, w := range words { + for _, c := range w { + cnt[c-'a']++ } } n := len(words) - for _, count := range counter { - if count%n != 0 { + for _, v := range cnt { + if v%n != 0 { return false } } @@ -144,20 +152,33 @@ func makeEqual(words []string) bool { ```ts function makeEqual(words: string[]): boolean { - let n = words.length; - let letters = new Array(26).fill(0); - for (let word of words) { - for (let i = 0; i < word.length; ++i) { - ++letters[word.charCodeAt(i) - 97]; + const cnt: Record = {}; + for (const w of words) { + for (const c of w) { + cnt[c] = (cnt[c] || 0) + 1; } } + const n = words.length; + return Object.values(cnt).every(v => v % n === 0); +} +``` + +#### Rust - for (let i = 0; i < letters.length; ++i) { - if (letters[i] % n != 0) { - return false; +```rust +impl Solution { + pub fn make_equal(words: Vec) -> bool { + let mut cnt = std::collections::HashMap::new(); + + for word in words.iter() { + for c in word.chars() { + *cnt.entry(c).or_insert(0) += 1; + } } + + let n = words.len(); + cnt.values().all(|&v| v % n == 0) } - return true; } ``` diff --git a/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/README_EN.md b/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/README_EN.md index 48c1b5e38ffc6..b348b02020ca8 100644 --- a/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/README_EN.md +++ b/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/README_EN.md @@ -60,7 +60,13 @@ All the strings are now equal to "abc", so return true. -### Solution 1 +### Solution 1: Counting + +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. + +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. + +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$. @@ -69,12 +75,12 @@ All the strings are now equal to "abc", so return true. ```python class Solution: def makeEqual(self, words: List[str]) -> bool: - counter = Counter() - for word in words: - for c in word: - counter[c] += 1 + cnt = Counter() + for w in words: + for c in w: + cnt[c] += 1 n = len(words) - return all(count % n == 0 for count in counter.values()) + return all(v % n == 0 for v in cnt.values()) ``` #### Java @@ -82,15 +88,15 @@ class Solution: ```java class Solution { public boolean makeEqual(String[] words) { - int[] counter = new int[26]; - for (String word : words) { - for (char c : word.toCharArray()) { - ++counter[c - 'a']; + int[] cnt = new int[26]; + for (var w : words) { + for (char c : w.toCharArray()) { + ++cnt[c - 'a']; } } int n = words.length; - for (int i = 0; i < 26; ++i) { - if (counter[i] % n != 0) { + for (int v : cnt) { + if (v % n != 0) { return false; } } @@ -105,15 +111,17 @@ class Solution { class Solution { public: bool makeEqual(vector& words) { - vector counter(26, 0); - for (string word : words) { - for (char c : word) { - ++counter[c - 'a']; + int cnt[26]{}; + for (const auto& w : words) { + for (const auto& c : w) { + ++cnt[c - 'a']; } } int n = words.size(); - for (int count : counter) { - if (count % n != 0) return false; + for (int i = 0; i < 26; ++i) { + if (cnt[i] % n != 0) { + return false; + } } return true; } @@ -124,15 +132,15 @@ public: ```go func makeEqual(words []string) bool { - counter := [26]int{} - for _, word := range words { - for _, c := range word { - counter[c-'a']++ + cnt := [26]int{} + for _, w := range words { + for _, c := range w { + cnt[c-'a']++ } } n := len(words) - for _, count := range counter { - if count%n != 0 { + for _, v := range cnt { + if v%n != 0 { return false } } @@ -144,20 +152,33 @@ func makeEqual(words []string) bool { ```ts function makeEqual(words: string[]): boolean { - let n = words.length; - let letters = new Array(26).fill(0); - for (let word of words) { - for (let i = 0; i < word.length; ++i) { - ++letters[word.charCodeAt(i) - 97]; + const cnt: Record = {}; + for (const w of words) { + for (const c of w) { + cnt[c] = (cnt[c] || 0) + 1; } } + const n = words.length; + return Object.values(cnt).every(v => v % n === 0); +} +``` + +#### Rust - for (let i = 0; i < letters.length; ++i) { - if (letters[i] % n != 0) { - return false; +```rust +impl Solution { + pub fn make_equal(words: Vec) -> bool { + let mut cnt = std::collections::HashMap::new(); + + for word in words.iter() { + for c in word.chars() { + *cnt.entry(c).or_insert(0) += 1; + } } + + let n = words.len(); + cnt.values().all(|&v| v % n == 0) } - return true; } ``` diff --git a/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/Solution.cpp b/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/Solution.cpp index 3546cf5a47a77..67f00076409f5 100644 --- a/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/Solution.cpp +++ b/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/Solution.cpp @@ -1,16 +1,18 @@ class Solution { public: bool makeEqual(vector& words) { - vector counter(26, 0); - for (string word : words) { - for (char c : word) { - ++counter[c - 'a']; + int cnt[26]{}; + for (const auto& w : words) { + for (const auto& c : w) { + ++cnt[c - 'a']; } } int n = words.size(); - for (int count : counter) { - if (count % n != 0) return false; + for (int i = 0; i < 26; ++i) { + if (cnt[i] % n != 0) { + return false; + } } return true; } -}; \ No newline at end of file +}; diff --git a/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/Solution.go b/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/Solution.go index ed8028a7209ae..d9950d97b7147 100644 --- a/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/Solution.go +++ b/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/Solution.go @@ -1,15 +1,15 @@ func makeEqual(words []string) bool { - counter := [26]int{} - for _, word := range words { - for _, c := range word { - counter[c-'a']++ + cnt := [26]int{} + for _, w := range words { + for _, c := range w { + cnt[c-'a']++ } } n := len(words) - for _, count := range counter { - if count%n != 0 { + for _, v := range cnt { + if v%n != 0 { return false } } return true -} \ No newline at end of file +} diff --git a/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/Solution.java b/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/Solution.java index f25cbb7a1a3f0..2376044cae3bd 100644 --- a/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/Solution.java +++ b/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/Solution.java @@ -1,17 +1,17 @@ class Solution { public boolean makeEqual(String[] words) { - int[] counter = new int[26]; - for (String word : words) { - for (char c : word.toCharArray()) { - ++counter[c - 'a']; + int[] cnt = new int[26]; + for (var w : words) { + for (char c : w.toCharArray()) { + ++cnt[c - 'a']; } } int n = words.length; - for (int i = 0; i < 26; ++i) { - if (counter[i] % n != 0) { + for (int v : cnt) { + if (v % n != 0) { return false; } } return true; } -} \ No newline at end of file +} diff --git a/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/Solution.py b/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/Solution.py index d6648dbd82e6a..f45f5aaedfa29 100644 --- a/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/Solution.py +++ b/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/Solution.py @@ -1,8 +1,8 @@ class Solution: def makeEqual(self, words: List[str]) -> bool: - counter = Counter() - for word in words: - for c in word: - counter[c] += 1 + cnt = Counter() + for w in words: + for c in w: + cnt[c] += 1 n = len(words) - return all(count % n == 0 for count in counter.values()) + return all(v % n == 0 for v in cnt.values()) diff --git a/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/Solution.rs b/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/Solution.rs new file mode 100644 index 0000000000000..e2fea4ecd7aea --- /dev/null +++ b/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/Solution.rs @@ -0,0 +1,14 @@ +impl Solution { + pub fn make_equal(words: Vec) -> bool { + let mut cnt = std::collections::HashMap::new(); + + for word in words.iter() { + for c in word.chars() { + *cnt.entry(c).or_insert(0) += 1; + } + } + + let n = words.len(); + cnt.values().all(|&v| v % n == 0) + } +} diff --git a/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/Solution.ts b/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/Solution.ts index e4ed6a4f52aa4..b0339fbaba8b2 100644 --- a/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/Solution.ts +++ b/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/Solution.ts @@ -1,16 +1,10 @@ function makeEqual(words: string[]): boolean { - let n = words.length; - let letters = new Array(26).fill(0); - for (let word of words) { - for (let i = 0; i < word.length; ++i) { - ++letters[word.charCodeAt(i) - 97]; + const cnt: Record = {}; + for (const w of words) { + for (const c of w) { + cnt[c] = (cnt[c] || 0) + 1; } } - - for (let i = 0; i < letters.length; ++i) { - if (letters[i] % n != 0) { - return false; - } - } - return true; + const n = words.length; + return Object.values(cnt).every(v => v % n === 0); }