Skip to content

Commit f2fa675

Browse files
committedJun 9, 2023
feat: add solutions to lc problem: No.1941
No.1941.Check if All Characters Have Equal Number of Occurrences
1 parent b8630f5 commit f2fa675

File tree

6 files changed

+168
-66
lines changed

6 files changed

+168
-66
lines changed
 

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

+63-22
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@
4040

4141
<!-- 这里可写通用的实现逻辑 -->
4242

43+
**方法一:计数**
44+
45+
我们用一个哈希表或一个长度为 $26$ 的数组 $cnt$ 记录字符串 $s$ 中每个字符出现的次数。
46+
47+
接下来遍历 $cnt$ 中的每个值,判断所有非零值是否相等即可。
48+
49+
时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 是字符串 $s$ 的长度;而 $C$ 是字符集大小,本题中字符集为小写英文字母,因此 $C=26$。
50+
4351
<!-- tabs:start -->
4452

4553
### **Python3**
@@ -61,18 +69,17 @@ class Solution:
6169
class Solution {
6270
public boolean areOccurrencesEqual(String s) {
6371
int[] cnt = new int[26];
64-
for (char c : s.toCharArray()) {
65-
++cnt[c - 'a'];
72+
for (int i = 0; i < s.length(); ++i) {
73+
++cnt[s.charAt(i) - 'a'];
6674
}
67-
int t = 0;
75+
int x = 0;
6876
for (int v : cnt) {
69-
if (v == 0) {
70-
continue;
71-
}
72-
if (t == 0) {
73-
t = v;
74-
} else if (t != v) {
75-
return false;
77+
if (v > 0) {
78+
if (x == 0) {
79+
x = v;
80+
} else if (x != v) {
81+
return false;
82+
}
7683
}
7784
}
7885
return true;
@@ -86,12 +93,21 @@ class Solution {
8693
class Solution {
8794
public:
8895
bool areOccurrencesEqual(string s) {
89-
vector<int> cnt(26);
90-
for (char& c : s) ++cnt[c - 'a'];
91-
unordered_set<int> ss;
92-
for (int& v : cnt)
93-
if (v) ss.insert(v);
94-
return ss.size() == 1;
96+
int cnt[26]{};
97+
for (char& c : s) {
98+
++cnt[c - 'a'];
99+
}
100+
int x = 0;
101+
for (int& v : cnt) {
102+
if (v) {
103+
if (!x) {
104+
x = v;
105+
} else if (x != v) {
106+
return false;
107+
}
108+
}
109+
}
110+
return true;
95111
}
96112
};
97113
```
@@ -100,18 +116,43 @@ public:
100116
101117
```go
102118
func areOccurrencesEqual(s string) bool {
103-
cnt := make([]int, 26)
119+
cnt := [26]int{}
104120
for _, c := range s {
105121
cnt[c-'a']++
106122
}
107-
ss := map[int]bool{}
123+
x := 0
108124
for _, v := range cnt {
109-
if v == 0 {
110-
continue
125+
if v > 0 {
126+
if x == 0 {
127+
x = v
128+
} else if x != v {
129+
return false
130+
}
111131
}
112-
ss[v] = true
113132
}
114-
return len(ss) == 1
133+
return true
134+
}
135+
```
136+
137+
### **TypeScript**
138+
139+
```ts
140+
function areOccurrencesEqual(s: string): boolean {
141+
const cnt: number[] = new Array(26).fill(0);
142+
for (const c of s) {
143+
++cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)];
144+
}
145+
let x = 0;
146+
for (const v of cnt) {
147+
if (v) {
148+
if (!x) {
149+
x = v;
150+
} else if (x !== v) {
151+
return false;
152+
}
153+
}
154+
}
155+
return true;
115156
}
116157
```
117158

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

+55-22
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,17 @@ class Solution:
5353
class Solution {
5454
public boolean areOccurrencesEqual(String s) {
5555
int[] cnt = new int[26];
56-
for (char c : s.toCharArray()) {
57-
++cnt[c - 'a'];
56+
for (int i = 0; i < s.length(); ++i) {
57+
++cnt[s.charAt(i) - 'a'];
5858
}
59-
int t = 0;
59+
int x = 0;
6060
for (int v : cnt) {
61-
if (v == 0) {
62-
continue;
63-
}
64-
if (t == 0) {
65-
t = v;
66-
} else if (t != v) {
67-
return false;
61+
if (v > 0) {
62+
if (x == 0) {
63+
x = v;
64+
} else if (x != v) {
65+
return false;
66+
}
6867
}
6968
}
7069
return true;
@@ -78,12 +77,21 @@ class Solution {
7877
class Solution {
7978
public:
8079
bool areOccurrencesEqual(string s) {
81-
vector<int> cnt(26);
82-
for (char& c : s) ++cnt[c - 'a'];
83-
unordered_set<int> ss;
84-
for (int& v : cnt)
85-
if (v) ss.insert(v);
86-
return ss.size() == 1;
80+
int cnt[26]{};
81+
for (char& c : s) {
82+
++cnt[c - 'a'];
83+
}
84+
int x = 0;
85+
for (int& v : cnt) {
86+
if (v) {
87+
if (!x) {
88+
x = v;
89+
} else if (x != v) {
90+
return false;
91+
}
92+
}
93+
}
94+
return true;
8795
}
8896
};
8997
```
@@ -92,18 +100,43 @@ public:
92100
93101
```go
94102
func areOccurrencesEqual(s string) bool {
95-
cnt := make([]int, 26)
103+
cnt := [26]int{}
96104
for _, c := range s {
97105
cnt[c-'a']++
98106
}
99-
ss := map[int]bool{}
107+
x := 0
100108
for _, v := range cnt {
101-
if v == 0 {
102-
continue
109+
if v > 0 {
110+
if x == 0 {
111+
x = v
112+
} else if x != v {
113+
return false
114+
}
103115
}
104-
ss[v] = true
105116
}
106-
return len(ss) == 1
117+
return true
118+
}
119+
```
120+
121+
### **TypeScript**
122+
123+
```ts
124+
function areOccurrencesEqual(s: string): boolean {
125+
const cnt: number[] = new Array(26).fill(0);
126+
for (const c of s) {
127+
++cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)];
128+
}
129+
let x = 0;
130+
for (const v of cnt) {
131+
if (v) {
132+
if (!x) {
133+
x = v;
134+
} else if (x !== v) {
135+
return false;
136+
}
137+
}
138+
}
139+
return true;
107140
}
108141
```
109142

Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
class Solution {
22
public:
33
bool areOccurrencesEqual(string s) {
4-
vector<int> cnt(26);
5-
for (char& c : s) ++cnt[c - 'a'];
6-
unordered_set<int> ss;
7-
for (int& v : cnt)
8-
if (v) ss.insert(v);
9-
return ss.size() == 1;
4+
int cnt[26]{};
5+
for (char& c : s) {
6+
++cnt[c - 'a'];
7+
}
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+
}
16+
}
17+
}
18+
return true;
1019
}
1120
};
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
func areOccurrencesEqual(s string) bool {
2-
cnt := make([]int, 26)
2+
cnt := [26]int{}
33
for _, c := range s {
44
cnt[c-'a']++
55
}
6-
ss := map[int]bool{}
6+
x := 0
77
for _, v := range cnt {
8-
if v == 0 {
9-
continue
8+
if v > 0 {
9+
if x == 0 {
10+
x = v
11+
} else if x != v {
12+
return false
13+
}
1014
}
11-
ss[v] = true
1215
}
13-
return len(ss) == 1
16+
return true
1417
}

‎solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/Solution.java

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
class Solution {
22
public boolean areOccurrencesEqual(String s) {
33
int[] cnt = new int[26];
4-
for (char c : s.toCharArray()) {
5-
++cnt[c - 'a'];
4+
for (int i = 0; i < s.length(); ++i) {
5+
++cnt[s.charAt(i) - 'a'];
66
}
7-
int t = 0;
7+
int x = 0;
88
for (int v : cnt) {
9-
if (v == 0) {
10-
continue;
11-
}
12-
if (t == 0) {
13-
t = v;
14-
} else if (t != v) {
15-
return false;
9+
if (v > 0) {
10+
if (x == 0) {
11+
x = v;
12+
} else if (x != v) {
13+
return false;
14+
}
1615
}
1716
}
1817
return true;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function areOccurrencesEqual(s: string): boolean {
2+
const cnt: number[] = new Array(26).fill(0);
3+
for (const c of s) {
4+
++cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)];
5+
}
6+
let x = 0;
7+
for (const v of cnt) {
8+
if (v) {
9+
if (!x) {
10+
x = v;
11+
} else if (x !== v) {
12+
return false;
13+
}
14+
}
15+
}
16+
return true;
17+
}

0 commit comments

Comments
 (0)
Please sign in to comment.