Skip to content

Commit e722e68

Browse files
committed
feat: add solutions to lc problem: No.1941. Check if All Characters Have
Equal Number of Occurrences
1 parent ae5ea5d commit e722e68

File tree

4 files changed

+93
-4
lines changed

4 files changed

+93
-4
lines changed

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

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,44 @@
4848
<!-- 这里可写当前语言的特殊实现逻辑 -->
4949

5050
```python
51-
51+
class Solution:
52+
def areOccurrencesEqual(self, s: str) -> bool:
53+
counter = collections.Counter(s)
54+
cnt = -1
55+
for c, times in counter.items():
56+
if cnt == -1:
57+
cnt = times
58+
elif cnt != times:
59+
return False
60+
return True
5261
```
5362

5463
### **Java**
5564

5665
<!-- 这里可写当前语言的特殊实现逻辑 -->
5766

5867
```java
59-
68+
class Solution {
69+
public boolean areOccurrencesEqual(String s) {
70+
int[] counter = new int[26];
71+
for (char c : s.toCharArray()) {
72+
++counter[c - 'a'];
73+
}
74+
int cnt = -1;
75+
for (int i = 0; i < 26; ++i) {
76+
if (counter[i] == 0) {
77+
continue;
78+
}
79+
80+
if (cnt == -1) {
81+
cnt = counter[i];
82+
} else if (cnt != counter[i]) {
83+
return false;
84+
}
85+
}
86+
return true;
87+
}
88+
}
6089
```
6190

6291
### **...**

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

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,42 @@
4242
### **Python3**
4343

4444
```python
45-
45+
class Solution:
46+
def areOccurrencesEqual(self, s: str) -> bool:
47+
counter = collections.Counter(s)
48+
cnt = -1
49+
for c, times in counter.items():
50+
if cnt == -1:
51+
cnt = times
52+
elif cnt != times:
53+
return False
54+
return True
4655
```
4756

4857
### **Java**
4958

5059
```java
51-
60+
class Solution {
61+
public boolean areOccurrencesEqual(String s) {
62+
int[] counter = new int[26];
63+
for (char c : s.toCharArray()) {
64+
++counter[c - 'a'];
65+
}
66+
int cnt = -1;
67+
for (int i = 0; i < 26; ++i) {
68+
if (counter[i] == 0) {
69+
continue;
70+
}
71+
72+
if (cnt == -1) {
73+
cnt = counter[i];
74+
} else if (cnt != counter[i]) {
75+
return false;
76+
}
77+
}
78+
return true;
79+
}
80+
}
5281
```
5382

5483
### **...**
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public boolean areOccurrencesEqual(String s) {
3+
int[] counter = new int[26];
4+
for (char c : s.toCharArray()) {
5+
++counter[c - 'a'];
6+
}
7+
int cnt = -1;
8+
for (int i = 0; i < 26; ++i) {
9+
if (counter[i] == 0) {
10+
continue;
11+
}
12+
13+
if (cnt == -1) {
14+
cnt = counter[i];
15+
} else if (cnt != counter[i]) {
16+
return false;
17+
}
18+
}
19+
return true;
20+
}
21+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def areOccurrencesEqual(self, s: str) -> bool:
3+
counter = collections.Counter(s)
4+
cnt = -1
5+
for c, times in counter.items():
6+
if cnt == -1:
7+
cnt = times
8+
elif cnt != times:
9+
return False
10+
return True

0 commit comments

Comments
 (0)