Skip to content

Commit 33cf9f8

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

File tree

6 files changed

+122
-51
lines changed

6 files changed

+122
-51
lines changed

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

+44-17
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,8 @@
4949
```python
5050
class Solution:
5151
def areOccurrencesEqual(self, s: str) -> bool:
52-
counter = Counter(s)
53-
cnt = -1
54-
for c, times in counter.items():
55-
if cnt == -1:
56-
cnt = times
57-
elif cnt != times:
58-
return False
59-
return True
52+
cnt = Counter(s)
53+
return len(set(cnt.values())) == 1
6054
```
6155

6256
### **Java**
@@ -66,19 +60,18 @@ class Solution:
6660
```java
6761
class Solution {
6862
public boolean areOccurrencesEqual(String s) {
69-
int[] counter = new int[26];
63+
int[] cnt = new int[26];
7064
for (char c : s.toCharArray()) {
71-
++counter[c - 'a'];
65+
++cnt[c - 'a'];
7266
}
73-
int cnt = -1;
74-
for (int i = 0; i < 26; ++i) {
75-
if (counter[i] == 0) {
67+
int t = 0;
68+
for (int v : cnt) {
69+
if (v == 0) {
7670
continue;
7771
}
78-
79-
if (cnt == -1) {
80-
cnt = counter[i];
81-
} else if (cnt != counter[i]) {
72+
if (t == 0) {
73+
t = v;
74+
} else if (t != v) {
8275
return false;
8376
}
8477
}
@@ -87,6 +80,40 @@ class Solution {
8780
}
8881
```
8982

83+
### **C++**
84+
85+
```cpp
86+
class Solution {
87+
public:
88+
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) if (v) ss.insert(v);
93+
return ss.size() == 1;
94+
}
95+
};
96+
```
97+
98+
### **Go**
99+
100+
```go
101+
func areOccurrencesEqual(s string) bool {
102+
cnt := make([]int, 26)
103+
for _, c := range s {
104+
cnt[c-'a']++
105+
}
106+
ss := map[int]bool{}
107+
for _, v := range cnt {
108+
if v == 0 {
109+
continue
110+
}
111+
ss[v] = true
112+
}
113+
return len(ss) == 1
114+
}
115+
```
116+
90117
### **...**
91118

92119
```

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

+44-17
Original file line numberDiff line numberDiff line change
@@ -43,34 +43,27 @@
4343
```python
4444
class Solution:
4545
def areOccurrencesEqual(self, s: str) -> bool:
46-
counter = Counter(s)
47-
cnt = -1
48-
for c, times in counter.items():
49-
if cnt == -1:
50-
cnt = times
51-
elif cnt != times:
52-
return False
53-
return True
46+
cnt = Counter(s)
47+
return len(set(cnt.values())) == 1
5448
```
5549

5650
### **Java**
5751

5852
```java
5953
class Solution {
6054
public boolean areOccurrencesEqual(String s) {
61-
int[] counter = new int[26];
55+
int[] cnt = new int[26];
6256
for (char c : s.toCharArray()) {
63-
++counter[c - 'a'];
57+
++cnt[c - 'a'];
6458
}
65-
int cnt = -1;
66-
for (int i = 0; i < 26; ++i) {
67-
if (counter[i] == 0) {
59+
int t = 0;
60+
for (int v : cnt) {
61+
if (v == 0) {
6862
continue;
6963
}
70-
71-
if (cnt == -1) {
72-
cnt = counter[i];
73-
} else if (cnt != counter[i]) {
64+
if (t == 0) {
65+
t = v;
66+
} else if (t != v) {
7467
return false;
7568
}
7669
}
@@ -79,6 +72,40 @@ class Solution {
7972
}
8073
```
8174

75+
### **C++**
76+
77+
```cpp
78+
class Solution {
79+
public:
80+
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) if (v) ss.insert(v);
85+
return ss.size() == 1;
86+
}
87+
};
88+
```
89+
90+
### **Go**
91+
92+
```go
93+
func areOccurrencesEqual(s string) bool {
94+
cnt := make([]int, 26)
95+
for _, c := range s {
96+
cnt[c-'a']++
97+
}
98+
ss := map[int]bool{}
99+
for _, v := range cnt {
100+
if v == 0 {
101+
continue
102+
}
103+
ss[v] = true
104+
}
105+
return len(ss) == 1
106+
}
107+
```
108+
82109
### **...**
83110

84111
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public:
3+
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) if (v) ss.insert(v);
8+
return ss.size() == 1;
9+
}
10+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func areOccurrencesEqual(s string) bool {
2+
cnt := make([]int, 26)
3+
for _, c := range s {
4+
cnt[c-'a']++
5+
}
6+
ss := map[int]bool{}
7+
for _, v := range cnt {
8+
if v == 0 {
9+
continue
10+
}
11+
ss[v] = true
12+
}
13+
return len(ss) == 1
14+
}

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

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
class Solution {
22
public boolean areOccurrencesEqual(String s) {
3-
int[] counter = new int[26];
3+
int[] cnt = new int[26];
44
for (char c : s.toCharArray()) {
5-
++counter[c - 'a'];
5+
++cnt[c - 'a'];
66
}
7-
int cnt = -1;
8-
for (int i = 0; i < 26; ++i) {
9-
if (counter[i] == 0) {
7+
int t = 0;
8+
for (int v : cnt) {
9+
if (v == 0) {
1010
continue;
1111
}
12-
13-
if (cnt == -1) {
14-
cnt = counter[i];
15-
} else if (cnt != counter[i]) {
12+
if (t == 0) {
13+
t = v;
14+
} else if (t != v) {
1615
return false;
1716
}
1817
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
class Solution:
22
def areOccurrencesEqual(self, s: str) -> bool:
3-
counter = 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
3+
cnt = Counter(s)
4+
return len(set(cnt.values())) == 1

0 commit comments

Comments
 (0)