File tree 3 files changed +27
-24
lines changed
solution/1000-1099/1002.Find Common Characters
3 files changed +27
-24
lines changed Original file line number Diff line number Diff line change @@ -164,23 +164,24 @@ func commonChars(words []string) (ans []string) {
164
164
165
165
``` ts
166
166
function commonChars(words : string []): string [] {
167
- const freq: number [] = new Array (26 ).fill (10000 );
167
+ const freq: number [] = Array (26 ).fill (Number .POSITIVE_INFINITY);
168
+ const aCode = ' a' .charCodeAt (0 );
168
169
for (const word of words ) {
169
- const t: number [] = new Array (26 ).fill (0 );
170
- for (const c of word . split ( ' ' ) ) {
171
- ++ t [c .charCodeAt (0 ) - ' a ' . charCodeAt ( 0 ) ];
170
+ const t: number [] = Array (26 ).fill (0 );
171
+ for (const c of word ) {
172
+ ++ t [c .charCodeAt (0 ) - aCode ];
172
173
}
173
174
for (let i = 0 ; i < 26 ; ++ i ) {
174
175
freq [i ] = Math .min (freq [i ], t [i ]);
175
176
}
176
177
}
177
- const res : string [] = [];
178
+ const ans : string [] = [];
178
179
for (let i = 0 ; i < 26 ; ++ i ) {
179
- while (freq [i ]-- > 0 ) {
180
- res .push (String .fromCharCode (i + ' a ' . charCodeAt ( 0 ) ));
180
+ if (freq [i ]) {
181
+ ans .push (... String .fromCharCode (i + aCode ). repeat ( freq [ i ] ));
181
182
}
182
183
}
183
- return res ;
184
+ return ans ;
184
185
}
185
186
```
186
187
Original file line number Diff line number Diff line change @@ -151,23 +151,24 @@ func commonChars(words []string) (ans []string) {
151
151
152
152
``` ts
153
153
function commonChars(words : string []): string [] {
154
- const freq: number [] = new Array (26 ).fill (10000 );
154
+ const freq: number [] = Array (26 ).fill (Number .POSITIVE_INFINITY);
155
+ const aCode = ' a' .charCodeAt (0 );
155
156
for (const word of words ) {
156
- const t: number [] = new Array (26 ).fill (0 );
157
- for (const c of word . split ( ' ' ) ) {
158
- ++ t [c .charCodeAt (0 ) - ' a ' . charCodeAt ( 0 ) ];
157
+ const t: number [] = Array (26 ).fill (0 );
158
+ for (const c of word ) {
159
+ ++ t [c .charCodeAt (0 ) - aCode ];
159
160
}
160
161
for (let i = 0 ; i < 26 ; ++ i ) {
161
162
freq [i ] = Math .min (freq [i ], t [i ]);
162
163
}
163
164
}
164
- const res : string [] = [];
165
+ const ans : string [] = [];
165
166
for (let i = 0 ; i < 26 ; ++ i ) {
166
- while (freq [i ]-- > 0 ) {
167
- res .push (String .fromCharCode (i + ' a ' . charCodeAt ( 0 ) ));
167
+ if (freq [i ]) {
168
+ ans .push (... String .fromCharCode (i + aCode ). repeat ( freq [ i ] ));
168
169
}
169
170
}
170
- return res ;
171
+ return ans ;
171
172
}
172
173
```
173
174
Original file line number Diff line number Diff line change 1
1
function commonChars ( words : string [ ] ) : string [ ] {
2
- const freq : number [ ] = new Array ( 26 ) . fill ( 10000 ) ;
2
+ const freq : number [ ] = Array ( 26 ) . fill ( Number . POSITIVE_INFINITY ) ;
3
+ const aCode = 'a' . charCodeAt ( 0 ) ;
3
4
for ( const word of words ) {
4
- const t : number [ ] = new Array ( 26 ) . fill ( 0 ) ;
5
- for ( const c of word . split ( '' ) ) {
6
- ++ t [ c . charCodeAt ( 0 ) - 'a' . charCodeAt ( 0 ) ] ;
5
+ const t : number [ ] = Array ( 26 ) . fill ( 0 ) ;
6
+ for ( const c of word ) {
7
+ ++ t [ c . charCodeAt ( 0 ) - aCode ] ;
7
8
}
8
9
for ( let i = 0 ; i < 26 ; ++ i ) {
9
10
freq [ i ] = Math . min ( freq [ i ] , t [ i ] ) ;
10
11
}
11
12
}
12
- const res : string [ ] = [ ] ;
13
+ const ans : string [ ] = [ ] ;
13
14
for ( let i = 0 ; i < 26 ; ++ i ) {
14
- while ( freq [ i ] -- > 0 ) {
15
- res . push ( String . fromCharCode ( i + 'a' . charCodeAt ( 0 ) ) ) ;
15
+ if ( freq [ i ] ) {
16
+ ans . push ( ... String . fromCharCode ( i + aCode ) . repeat ( freq [ i ] ) ) ;
16
17
}
17
18
}
18
- return res ;
19
+ return ans ;
19
20
}
You can’t perform that action at this time.
0 commit comments