File tree 3 files changed +24
-11
lines changed
solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences
3 files changed +24
-11
lines changed Original file line number Diff line number Diff line change @@ -156,6 +156,17 @@ function areOccurrencesEqual(s: string): boolean {
156
156
}
157
157
```
158
158
159
+ ``` ts
160
+ function areOccurrencesEqual(s : string ): boolean {
161
+ const cnt: number [] = new Array (26 ).fill (0 );
162
+ for (const c of s ) {
163
+ ++ cnt [c .charCodeAt (0 ) - ' a' .charCodeAt (0 )];
164
+ }
165
+ const x = cnt .find (v => v );
166
+ return cnt .every (v => ! v || v === x );
167
+ }
168
+ ```
169
+
159
170
### ** PHP**
160
171
161
172
``` php
Original file line number Diff line number Diff line change @@ -140,6 +140,17 @@ function areOccurrencesEqual(s: string): boolean {
140
140
}
141
141
```
142
142
143
+ ``` ts
144
+ function areOccurrencesEqual(s : string ): boolean {
145
+ const cnt: number [] = new Array (26 ).fill (0 );
146
+ for (const c of s ) {
147
+ ++ cnt [c .charCodeAt (0 ) - ' a' .charCodeAt (0 )];
148
+ }
149
+ const x = cnt .find (v => v );
150
+ return cnt .every (v => ! v || v === x );
151
+ }
152
+ ```
153
+
143
154
### ** PHP**
144
155
145
156
``` php
Original file line number Diff line number Diff line change @@ -3,15 +3,6 @@ function areOccurrencesEqual(s: string): boolean {
3
3
for ( const c of s ) {
4
4
++ cnt [ c . charCodeAt ( 0 ) - 'a' . charCodeAt ( 0 ) ] ;
5
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 ;
6
+ const x = cnt . find ( v => v ) ;
7
+ return cnt . every ( v => ! v || v === x ) ;
17
8
}
You can’t perform that action at this time.
0 commit comments