diff --git a/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/README.md b/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/README.md index 2ac30846f2b5e..b038a4dba8898 100644 --- a/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/README.md +++ b/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/README.md @@ -58,11 +58,11 @@ tags: ### 方法一:计数 -我们用一个哈希表或一个长度为 $26$ 的数组 $cnt$ 记录字符串 $s$ 中每个字符出现的次数。 +我们用一个哈希表或者一个长度为 $26$ 的数组 $\textit{cnt}$ 记录字符串 $s$ 中每个字符出现的次数。 -接下来遍历 $cnt$ 中的每个值,判断所有非零值是否相等即可。 +接下来遍历 $\textit{cnt}$ 中的每个值,判断所有非零值是否相等即可。 -时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 是字符串 $s$ 的长度;而 $C$ 是字符集大小,本题中字符集为小写英文字母,因此 $C=26$。 +时间复杂度 $O(n)$,空间复杂度 $O(|\Sigma|)$。其中 $n$ 是字符串 $s$ 的长度;而 $\Sigma$ 是字符集大小,本题中字符集为小写英文字母,因此 $|\Sigma|=26$。 @@ -71,8 +71,7 @@ tags: ```python class Solution: def areOccurrencesEqual(self, s: str) -> bool: - cnt = Counter(s) - return len(set(cnt.values())) == 1 + return len(set(Counter(s).values())) == 1 ``` #### Java @@ -81,18 +80,18 @@ class Solution: class Solution { public boolean areOccurrencesEqual(String s) { int[] cnt = new int[26]; - for (int i = 0; i < s.length(); ++i) { - ++cnt[s.charAt(i) - 'a']; + for (char c : s.toCharArray()) { + ++cnt[c - 'a']; } - int x = 0; - for (int v : cnt) { - if (v > 0) { - if (x == 0) { - x = v; - } else if (x != v) { - return false; - } + int v = 0; + for (int x : cnt) { + if (x == 0) { + continue; + } + if (v > 0 && v != x) { + return false; } + v = x; } return true; } @@ -105,19 +104,19 @@ class Solution { class Solution { public: bool areOccurrencesEqual(string s) { - int cnt[26]{}; - for (char& c : s) { + vector cnt(26); + for (char c : s) { ++cnt[c - 'a']; } - int x = 0; - for (int& v : cnt) { - if (v) { - if (!x) { - x = v; - } else if (x != v) { - return false; - } + int v = 0; + for (int x : cnt) { + if (x == 0) { + continue; + } + if (v && v != x) { + return false; } + v = x; } return true; } @@ -132,15 +131,15 @@ func areOccurrencesEqual(s string) bool { for _, c := range s { cnt[c-'a']++ } - x := 0 - for _, v := range cnt { - if v > 0 { - if x == 0 { - x = v - } else if x != v { - return false - } + v := 0 + for _, x := range cnt { + if x == 0 { + continue } + if v > 0 && v != x { + return false + } + v = x } return true } @@ -150,21 +149,12 @@ func areOccurrencesEqual(s string) bool { ```ts function areOccurrencesEqual(s: string): boolean { - const cnt: number[] = new Array(26).fill(0); + const cnt: number[] = Array(26).fill(0); for (const c of s) { ++cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)]; } - let x = 0; - for (const v of cnt) { - if (v) { - if (!x) { - x = v; - } else if (x !== v) { - return false; - } - } - } - return true; + const v = cnt.find(v => v); + return cnt.every(x => !x || v === x); } ``` @@ -177,35 +167,22 @@ class Solution { * @return Boolean */ function areOccurrencesEqual($s) { + $cnt = array_fill(0, 26, 0); for ($i = 0; $i < strlen($s); $i++) { - $hashtable[$s[$i]] += 1; + $cnt[ord($s[$i]) - ord('a')]++; } - $rs = array_unique($hashtable); - return count($rs) === 1; - } -} -``` - - - - - - - -### 方法二 - - - -#### TypeScript - -```ts -function areOccurrencesEqual(s: string): boolean { - const cnt: number[] = new Array(26).fill(0); - for (const c of s) { - ++cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)]; + $v = 0; + foreach ($cnt as $x) { + if ($x == 0) { + continue; + } + if ($v && $v != $x) { + return false; + } + $v = $x; + } + return true; } - const x = cnt.find(v => v); - return cnt.every(v => !v || v === x); } ``` diff --git a/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/README_EN.md b/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/README_EN.md index cf0c8a7241854..deb93dc9d292a 100644 --- a/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/README_EN.md +++ b/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/README_EN.md @@ -56,7 +56,13 @@ tags: -### Solution 1 +### Solution 1: Counting + +We use a hash table or an array of length $26$ called $\textit{cnt}$ to record the number of occurrences of each character in the string $s$. + +Next, we traverse each value in $\textit{cnt}$ and check if all non-zero values are equal. + +The time complexity is $O(n)$, and the space complexity is $O(|\Sigma|)$. Here, $n$ is the length of the string $s$, and $\Sigma$ is the size of the character set. In this problem, the character set consists of lowercase English letters, so $|\Sigma|=26$. @@ -65,8 +71,7 @@ tags: ```python class Solution: def areOccurrencesEqual(self, s: str) -> bool: - cnt = Counter(s) - return len(set(cnt.values())) == 1 + return len(set(Counter(s).values())) == 1 ``` #### Java @@ -75,18 +80,18 @@ class Solution: class Solution { public boolean areOccurrencesEqual(String s) { int[] cnt = new int[26]; - for (int i = 0; i < s.length(); ++i) { - ++cnt[s.charAt(i) - 'a']; + for (char c : s.toCharArray()) { + ++cnt[c - 'a']; } - int x = 0; - for (int v : cnt) { - if (v > 0) { - if (x == 0) { - x = v; - } else if (x != v) { - return false; - } + int v = 0; + for (int x : cnt) { + if (x == 0) { + continue; + } + if (v > 0 && v != x) { + return false; } + v = x; } return true; } @@ -99,19 +104,19 @@ class Solution { class Solution { public: bool areOccurrencesEqual(string s) { - int cnt[26]{}; - for (char& c : s) { + vector cnt(26); + for (char c : s) { ++cnt[c - 'a']; } - int x = 0; - for (int& v : cnt) { - if (v) { - if (!x) { - x = v; - } else if (x != v) { - return false; - } + int v = 0; + for (int x : cnt) { + if (x == 0) { + continue; + } + if (v && v != x) { + return false; } + v = x; } return true; } @@ -126,15 +131,15 @@ func areOccurrencesEqual(s string) bool { for _, c := range s { cnt[c-'a']++ } - x := 0 - for _, v := range cnt { - if v > 0 { - if x == 0 { - x = v - } else if x != v { - return false - } + v := 0 + for _, x := range cnt { + if x == 0 { + continue } + if v > 0 && v != x { + return false + } + v = x } return true } @@ -144,21 +149,12 @@ func areOccurrencesEqual(s string) bool { ```ts function areOccurrencesEqual(s: string): boolean { - const cnt: number[] = new Array(26).fill(0); + const cnt: number[] = Array(26).fill(0); for (const c of s) { ++cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)]; } - let x = 0; - for (const v of cnt) { - if (v) { - if (!x) { - x = v; - } else if (x !== v) { - return false; - } - } - } - return true; + const v = cnt.find(v => v); + return cnt.every(x => !x || v === x); } ``` @@ -171,35 +167,22 @@ class Solution { * @return Boolean */ function areOccurrencesEqual($s) { + $cnt = array_fill(0, 26, 0); for ($i = 0; $i < strlen($s); $i++) { - $hashtable[$s[$i]] += 1; + $cnt[ord($s[$i]) - ord('a')]++; } - $rs = array_unique($hashtable); - return count($rs) === 1; - } -} -``` - - - - - - - -### Solution 2 - - - -#### TypeScript - -```ts -function areOccurrencesEqual(s: string): boolean { - const cnt: number[] = new Array(26).fill(0); - for (const c of s) { - ++cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)]; + $v = 0; + foreach ($cnt as $x) { + if ($x == 0) { + continue; + } + if ($v && $v != $x) { + return false; + } + $v = $x; + } + return true; } - const x = cnt.find(v => v); - return cnt.every(v => !v || v === x); } ``` diff --git a/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/Solution.cpp b/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/Solution.cpp index 74e620a91aac0..6e2e83f846fc5 100644 --- a/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/Solution.cpp +++ b/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/Solution.cpp @@ -1,20 +1,20 @@ class Solution { public: bool areOccurrencesEqual(string s) { - int cnt[26]{}; - for (char& c : s) { + vector cnt(26); + for (char c : s) { ++cnt[c - 'a']; } - int x = 0; - for (int& v : cnt) { - if (v) { - if (!x) { - x = v; - } else if (x != v) { - return false; - } + int v = 0; + for (int x : cnt) { + if (x == 0) { + continue; } + if (v && v != x) { + return false; + } + v = x; } return true; } -}; \ No newline at end of file +}; diff --git a/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/Solution.go b/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/Solution.go index 931e6358e7420..ef74b7635155b 100644 --- a/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/Solution.go +++ b/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/Solution.go @@ -3,15 +3,15 @@ func areOccurrencesEqual(s string) bool { for _, c := range s { cnt[c-'a']++ } - x := 0 - for _, v := range cnt { - if v > 0 { - if x == 0 { - x = v - } else if x != v { - return false - } + v := 0 + for _, x := range cnt { + if x == 0 { + continue } + if v > 0 && v != x { + return false + } + v = x } return true -} \ No newline at end of file +} diff --git a/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/Solution.java b/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/Solution.java index 6a1ce272078d1..70a936cbbacc8 100644 --- a/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/Solution.java +++ b/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/Solution.java @@ -1,19 +1,19 @@ class Solution { public boolean areOccurrencesEqual(String s) { int[] cnt = new int[26]; - for (int i = 0; i < s.length(); ++i) { - ++cnt[s.charAt(i) - 'a']; + for (char c : s.toCharArray()) { + ++cnt[c - 'a']; } - int x = 0; - for (int v : cnt) { - if (v > 0) { - if (x == 0) { - x = v; - } else if (x != v) { - return false; - } + int v = 0; + for (int x : cnt) { + if (x == 0) { + continue; } + if (v > 0 && v != x) { + return false; + } + v = x; } return true; } -} \ No newline at end of file +} diff --git a/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/Solution.php b/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/Solution.php index d07b8026ab486..44bd6db45c7a1 100644 --- a/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/Solution.php +++ b/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/Solution.php @@ -4,10 +4,20 @@ class Solution { * @return Boolean */ function areOccurrencesEqual($s) { + $cnt = array_fill(0, 26, 0); for ($i = 0; $i < strlen($s); $i++) { - $hashtable[$s[$i]] += 1; + $cnt[ord($s[$i]) - ord('a')]++; } - $rs = array_unique($hashtable); - return count($rs) === 1; + $v = 0; + foreach ($cnt as $x) { + if ($x == 0) { + continue; + } + if ($v && $v != $x) { + return false; + } + $v = $x; + } + return true; } } diff --git a/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/Solution.py b/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/Solution.py index 2ef28dc3a28b8..9075f0a92cdf8 100644 --- a/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/Solution.py +++ b/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/Solution.py @@ -1,4 +1,3 @@ class Solution: def areOccurrencesEqual(self, s: str) -> bool: - cnt = Counter(s) - return len(set(cnt.values())) == 1 + return len(set(Counter(s).values())) == 1 diff --git a/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/Solution.ts b/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/Solution.ts index 09bfeefef72ee..f4e741e4b17bd 100644 --- a/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/Solution.ts +++ b/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/Solution.ts @@ -1,17 +1,8 @@ function areOccurrencesEqual(s: string): boolean { - const cnt: number[] = new Array(26).fill(0); + const cnt: number[] = Array(26).fill(0); for (const c of s) { ++cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)]; } - let x = 0; - for (const v of cnt) { - if (v) { - if (!x) { - x = v; - } else if (x !== v) { - return false; - } - } - } - return true; + const v = cnt.find(v => v); + return cnt.every(x => !x || v === x); } diff --git a/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/Solution2.ts b/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/Solution2.ts deleted file mode 100644 index 678d4756e37bd..0000000000000 --- a/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/Solution2.ts +++ /dev/null @@ -1,8 +0,0 @@ -function areOccurrencesEqual(s: string): boolean { - const cnt: number[] = new Array(26).fill(0); - for (const c of s) { - ++cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)]; - } - const x = cnt.find(v => v); - return cnt.every(v => !v || v === x); -}