File tree Expand file tree Collapse file tree 4 files changed +93
-4
lines changed
solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences Expand file tree Collapse file tree 4 files changed +93
-4
lines changed Original file line number Diff line number Diff line change 48
48
<!-- 这里可写当前语言的特殊实现逻辑 -->
49
49
50
50
``` 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
52
61
```
53
62
54
63
### ** Java**
55
64
56
65
<!-- 这里可写当前语言的特殊实现逻辑 -->
57
66
58
67
``` 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
+ }
60
89
```
61
90
62
91
### ** ...**
Original file line number Diff line number Diff line change 42
42
### ** Python3**
43
43
44
44
``` 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
46
55
```
47
56
48
57
### ** Java**
49
58
50
59
``` 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
+ }
52
81
```
53
82
54
83
### ** ...**
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments