File tree 6 files changed +122
-51
lines changed
solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences
6 files changed +122
-51
lines changed Original file line number Diff line number Diff line change 49
49
``` python
50
50
class Solution :
51
51
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
60
54
```
61
55
62
56
### ** Java**
@@ -66,19 +60,18 @@ class Solution:
66
60
``` java
67
61
class Solution {
68
62
public boolean areOccurrencesEqual (String s ) {
69
- int [] counter = new int [26 ];
63
+ int [] cnt = new int [26 ];
70
64
for (char c : s. toCharArray()) {
71
- ++ counter [c - ' a' ];
65
+ ++ cnt [c - ' a' ];
72
66
}
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 ) {
76
70
continue ;
77
71
}
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) {
82
75
return false ;
83
76
}
84
77
}
@@ -87,6 +80,40 @@ class Solution {
87
80
}
88
81
```
89
82
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
+
90
117
### ** ...**
91
118
92
119
```
Original file line number Diff line number Diff line change 43
43
``` python
44
44
class Solution :
45
45
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
54
48
```
55
49
56
50
### ** Java**
57
51
58
52
``` java
59
53
class Solution {
60
54
public boolean areOccurrencesEqual (String s ) {
61
- int [] counter = new int [26 ];
55
+ int [] cnt = new int [26 ];
62
56
for (char c : s. toCharArray()) {
63
- ++ counter [c - ' a' ];
57
+ ++ cnt [c - ' a' ];
64
58
}
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 ) {
68
62
continue ;
69
63
}
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) {
74
67
return false ;
75
68
}
76
69
}
@@ -79,6 +72,40 @@ class Solution {
79
72
}
80
73
```
81
74
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
+
82
109
### ** ...**
83
110
84
111
```
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public boolean areOccurrencesEqual (String s ) {
3
- int [] counter = new int [26 ];
3
+ int [] cnt = new int [26 ];
4
4
for (char c : s .toCharArray ()) {
5
- ++counter [c - 'a' ];
5
+ ++cnt [c - 'a' ];
6
6
}
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 ) {
10
10
continue ;
11
11
}
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 ) {
16
15
return false ;
17
16
}
18
17
}
Original file line number Diff line number Diff line change 1
1
class Solution :
2
2
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
You can’t perform that action at this time.
0 commit comments