Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a2b7b6d

Browse files
authored
feat: add solutions to lc problem: No.1941 (doocs#3991)
No.1941.Check if All Characters Have Equal Number of Occurrences
1 parent 94ed2b0 commit a2b7b6d

File tree

9 files changed

+148
-196
lines changed

9 files changed

+148
-196
lines changed

solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/README.md

Lines changed: 48 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ tags:
5858

5959
### 方法一:计数
6060

61-
我们用一个哈希表或一个长度为 $26$ 的数组 $cnt$ 记录字符串 $s$ 中每个字符出现的次数。
61+
我们用一个哈希表或者一个长度为 $26$ 的数组 $\textit{cnt}$ 记录字符串 $s$ 中每个字符出现的次数。
6262

63-
接下来遍历 $cnt$ 中的每个值,判断所有非零值是否相等即可。
63+
接下来遍历 $\textit{cnt}$ 中的每个值,判断所有非零值是否相等即可。
6464

65-
时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 是字符串 $s$ 的长度;而 $C$ 是字符集大小,本题中字符集为小写英文字母,因此 $C=26$。
65+
时间复杂度 $O(n)$,空间复杂度 $O(|\Sigma|)$。其中 $n$ 是字符串 $s$ 的长度;而 $\Sigma$ 是字符集大小,本题中字符集为小写英文字母,因此 $|\Sigma|=26$。
6666

6767
<!-- tabs:start -->
6868

@@ -71,8 +71,7 @@ tags:
7171
```python
7272
class Solution:
7373
def areOccurrencesEqual(self, s: str) -> bool:
74-
cnt = Counter(s)
75-
return len(set(cnt.values())) == 1
74+
return len(set(Counter(s).values())) == 1
7675
```
7776

7877
#### Java
@@ -81,18 +80,18 @@ class Solution:
8180
class Solution {
8281
public boolean areOccurrencesEqual(String s) {
8382
int[] cnt = new int[26];
84-
for (int i = 0; i < s.length(); ++i) {
85-
++cnt[s.charAt(i) - 'a'];
83+
for (char c : s.toCharArray()) {
84+
++cnt[c - 'a'];
8685
}
87-
int x = 0;
88-
for (int v : cnt) {
89-
if (v > 0) {
90-
if (x == 0) {
91-
x = v;
92-
} else if (x != v) {
93-
return false;
94-
}
86+
int v = 0;
87+
for (int x : cnt) {
88+
if (x == 0) {
89+
continue;
90+
}
91+
if (v > 0 && v != x) {
92+
return false;
9593
}
94+
v = x;
9695
}
9796
return true;
9897
}
@@ -105,19 +104,19 @@ class Solution {
105104
class Solution {
106105
public:
107106
bool areOccurrencesEqual(string s) {
108-
int cnt[26]{};
109-
for (char& c : s) {
107+
vector<int> cnt(26);
108+
for (char c : s) {
110109
++cnt[c - 'a'];
111110
}
112-
int x = 0;
113-
for (int& v : cnt) {
114-
if (v) {
115-
if (!x) {
116-
x = v;
117-
} else if (x != v) {
118-
return false;
119-
}
111+
int v = 0;
112+
for (int x : cnt) {
113+
if (x == 0) {
114+
continue;
115+
}
116+
if (v && v != x) {
117+
return false;
120118
}
119+
v = x;
121120
}
122121
return true;
123122
}
@@ -132,15 +131,15 @@ func areOccurrencesEqual(s string) bool {
132131
for _, c := range s {
133132
cnt[c-'a']++
134133
}
135-
x := 0
136-
for _, v := range cnt {
137-
if v > 0 {
138-
if x == 0 {
139-
x = v
140-
} else if x != v {
141-
return false
142-
}
134+
v := 0
135+
for _, x := range cnt {
136+
if x == 0 {
137+
continue
143138
}
139+
if v > 0 && v != x {
140+
return false
141+
}
142+
v = x
144143
}
145144
return true
146145
}
@@ -150,21 +149,12 @@ func areOccurrencesEqual(s string) bool {
150149

151150
```ts
152151
function areOccurrencesEqual(s: string): boolean {
153-
const cnt: number[] = new Array(26).fill(0);
152+
const cnt: number[] = Array(26).fill(0);
154153
for (const c of s) {
155154
++cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)];
156155
}
157-
let x = 0;
158-
for (const v of cnt) {
159-
if (v) {
160-
if (!x) {
161-
x = v;
162-
} else if (x !== v) {
163-
return false;
164-
}
165-
}
166-
}
167-
return true;
156+
const v = cnt.find(v => v);
157+
return cnt.every(x => !x || v === x);
168158
}
169159
```
170160

@@ -177,35 +167,22 @@ class Solution {
177167
* @return Boolean
178168
*/
179169
function areOccurrencesEqual($s) {
170+
$cnt = array_fill(0, 26, 0);
180171
for ($i = 0; $i < strlen($s); $i++) {
181-
$hashtable[$s[$i]] += 1;
172+
$cnt[ord($s[$i]) - ord('a')]++;
182173
}
183-
$rs = array_unique($hashtable);
184-
return count($rs) === 1;
185-
}
186-
}
187-
```
188-
189-
<!-- tabs:end -->
190-
191-
<!-- solution:end -->
192-
193-
<!-- solution:start -->
194-
195-
### 方法二
196-
197-
<!-- tabs:start -->
198-
199-
#### TypeScript
200-
201-
```ts
202-
function areOccurrencesEqual(s: string): boolean {
203-
const cnt: number[] = new Array(26).fill(0);
204-
for (const c of s) {
205-
++cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)];
174+
$v = 0;
175+
foreach ($cnt as $x) {
176+
if ($x == 0) {
177+
continue;
178+
}
179+
if ($v && $v != $x) {
180+
return false;
181+
}
182+
$v = $x;
183+
}
184+
return true;
206185
}
207-
const x = cnt.find(v => v);
208-
return cnt.every(v => !v || v === x);
209186
}
210187
```
211188

solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/README_EN.md

Lines changed: 52 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ tags:
5656

5757
<!-- solution:start -->
5858

59-
### Solution 1
59+
### Solution 1: Counting
60+
61+
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$.
62+
63+
Next, we traverse each value in $\textit{cnt}$ and check if all non-zero values are equal.
64+
65+
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$.
6066

6167
<!-- tabs:start -->
6268

@@ -65,8 +71,7 @@ tags:
6571
```python
6672
class Solution:
6773
def areOccurrencesEqual(self, s: str) -> bool:
68-
cnt = Counter(s)
69-
return len(set(cnt.values())) == 1
74+
return len(set(Counter(s).values())) == 1
7075
```
7176

7277
#### Java
@@ -75,18 +80,18 @@ class Solution:
7580
class Solution {
7681
public boolean areOccurrencesEqual(String s) {
7782
int[] cnt = new int[26];
78-
for (int i = 0; i < s.length(); ++i) {
79-
++cnt[s.charAt(i) - 'a'];
83+
for (char c : s.toCharArray()) {
84+
++cnt[c - 'a'];
8085
}
81-
int x = 0;
82-
for (int v : cnt) {
83-
if (v > 0) {
84-
if (x == 0) {
85-
x = v;
86-
} else if (x != v) {
87-
return false;
88-
}
86+
int v = 0;
87+
for (int x : cnt) {
88+
if (x == 0) {
89+
continue;
90+
}
91+
if (v > 0 && v != x) {
92+
return false;
8993
}
94+
v = x;
9095
}
9196
return true;
9297
}
@@ -99,19 +104,19 @@ class Solution {
99104
class Solution {
100105
public:
101106
bool areOccurrencesEqual(string s) {
102-
int cnt[26]{};
103-
for (char& c : s) {
107+
vector<int> cnt(26);
108+
for (char c : s) {
104109
++cnt[c - 'a'];
105110
}
106-
int x = 0;
107-
for (int& v : cnt) {
108-
if (v) {
109-
if (!x) {
110-
x = v;
111-
} else if (x != v) {
112-
return false;
113-
}
111+
int v = 0;
112+
for (int x : cnt) {
113+
if (x == 0) {
114+
continue;
115+
}
116+
if (v && v != x) {
117+
return false;
114118
}
119+
v = x;
115120
}
116121
return true;
117122
}
@@ -126,15 +131,15 @@ func areOccurrencesEqual(s string) bool {
126131
for _, c := range s {
127132
cnt[c-'a']++
128133
}
129-
x := 0
130-
for _, v := range cnt {
131-
if v > 0 {
132-
if x == 0 {
133-
x = v
134-
} else if x != v {
135-
return false
136-
}
134+
v := 0
135+
for _, x := range cnt {
136+
if x == 0 {
137+
continue
137138
}
139+
if v > 0 && v != x {
140+
return false
141+
}
142+
v = x
138143
}
139144
return true
140145
}
@@ -144,21 +149,12 @@ func areOccurrencesEqual(s string) bool {
144149

145150
```ts
146151
function areOccurrencesEqual(s: string): boolean {
147-
const cnt: number[] = new Array(26).fill(0);
152+
const cnt: number[] = Array(26).fill(0);
148153
for (const c of s) {
149154
++cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)];
150155
}
151-
let x = 0;
152-
for (const v of cnt) {
153-
if (v) {
154-
if (!x) {
155-
x = v;
156-
} else if (x !== v) {
157-
return false;
158-
}
159-
}
160-
}
161-
return true;
156+
const v = cnt.find(v => v);
157+
return cnt.every(x => !x || v === x);
162158
}
163159
```
164160

@@ -171,35 +167,22 @@ class Solution {
171167
* @return Boolean
172168
*/
173169
function areOccurrencesEqual($s) {
170+
$cnt = array_fill(0, 26, 0);
174171
for ($i = 0; $i < strlen($s); $i++) {
175-
$hashtable[$s[$i]] += 1;
172+
$cnt[ord($s[$i]) - ord('a')]++;
176173
}
177-
$rs = array_unique($hashtable);
178-
return count($rs) === 1;
179-
}
180-
}
181-
```
182-
183-
<!-- tabs:end -->
184-
185-
<!-- solution:end -->
186-
187-
<!-- solution:start -->
188-
189-
### Solution 2
190-
191-
<!-- tabs:start -->
192-
193-
#### TypeScript
194-
195-
```ts
196-
function areOccurrencesEqual(s: string): boolean {
197-
const cnt: number[] = new Array(26).fill(0);
198-
for (const c of s) {
199-
++cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)];
174+
$v = 0;
175+
foreach ($cnt as $x) {
176+
if ($x == 0) {
177+
continue;
178+
}
179+
if ($v && $v != $x) {
180+
return false;
181+
}
182+
$v = $x;
183+
}
184+
return true;
200185
}
201-
const x = cnt.find(v => v);
202-
return cnt.every(v => !v || v === x);
203186
}
204187
```
205188

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
class Solution {
22
public:
33
bool areOccurrencesEqual(string s) {
4-
int cnt[26]{};
5-
for (char& c : s) {
4+
vector<int> cnt(26);
5+
for (char c : s) {
66
++cnt[c - 'a'];
77
}
8-
int x = 0;
9-
for (int& v : cnt) {
10-
if (v) {
11-
if (!x) {
12-
x = v;
13-
} else if (x != v) {
14-
return false;
15-
}
8+
int v = 0;
9+
for (int x : cnt) {
10+
if (x == 0) {
11+
continue;
1612
}
13+
if (v && v != x) {
14+
return false;
15+
}
16+
v = x;
1717
}
1818
return true;
1919
}
20-
};
20+
};

0 commit comments

Comments
 (0)