File tree 3 files changed +73
-0
lines changed
solution/1000-1099/1002.Find Common Characters
3 files changed +73
-0
lines changed Original file line number Diff line number Diff line change @@ -155,4 +155,34 @@ func min(a, b int) int {
155
155
}
156
156
```
157
157
158
+ ### ** TypeScript**
159
+
160
+ ``` ts
161
+ function commonChars(words : string []): string [] {
162
+ const freq: number [] = new Array (26 ).fill (10000 );
163
+ for (const word of words ) {
164
+ const t: number [] = new Array (26 ).fill (0 );
165
+ for (const c of word .split (' ' )) {
166
+ ++ t [c .charCodeAt (0 ) - ' a' .charCodeAt (0 )];
167
+ }
168
+ for (let i = 0 ; i < 26 ; ++ i ) {
169
+ freq [i ] = Math .min (freq [i ], t [i ]);
170
+ }
171
+ }
172
+ const res: string [] = [];
173
+ for (let i = 0 ; i < 26 ; ++ i ) {
174
+ while (freq [i ]-- > 0 ) {
175
+ res .push (String .fromCharCode (i + ' a' .charCodeAt (0 )));
176
+ }
177
+ }
178
+ return res ;
179
+ }
180
+ ```
181
+
182
+ ### ** ...**
183
+
184
+ ```
185
+
186
+ ```
187
+
158
188
<!-- tabs: end -->
Original file line number Diff line number Diff line change @@ -132,6 +132,30 @@ func min(a, b int) int {
132
132
}
133
133
```
134
134
135
+ ### ** TypeScript**
136
+
137
+ ``` ts
138
+ function commonChars(words : string []): string [] {
139
+ const freq: number [] = new Array (26 ).fill (10000 );
140
+ for (const word of words ) {
141
+ const t: number [] = new Array (26 ).fill (0 );
142
+ for (const c of word .split (' ' )) {
143
+ ++ t [c .charCodeAt (0 ) - ' a' .charCodeAt (0 )];
144
+ }
145
+ for (let i = 0 ; i < 26 ; ++ i ) {
146
+ freq [i ] = Math .min (freq [i ], t [i ]);
147
+ }
148
+ }
149
+ const res: string [] = [];
150
+ for (let i = 0 ; i < 26 ; ++ i ) {
151
+ while (freq [i ]-- > 0 ) {
152
+ res .push (String .fromCharCode (i + ' a' .charCodeAt (0 )));
153
+ }
154
+ }
155
+ return res ;
156
+ }
157
+ ```
158
+
135
159
### ** ...**
136
160
137
161
```
Original file line number Diff line number Diff line change
1
+ function commonChars ( words : string [ ] ) : string [ ] {
2
+ const freq : number [ ] = new Array ( 26 ) . fill ( 10000 ) ;
3
+ 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 ) ] ;
7
+ }
8
+ for ( let i = 0 ; i < 26 ; ++ i ) {
9
+ freq [ i ] = Math . min ( freq [ i ] , t [ i ] ) ;
10
+ }
11
+ }
12
+ const res : string [ ] = [ ] ;
13
+ for ( let i = 0 ; i < 26 ; ++ i ) {
14
+ while ( freq [ i ] -- > 0 ) {
15
+ res . push ( String . fromCharCode ( i + 'a' . charCodeAt ( 0 ) ) ) ;
16
+ }
17
+ }
18
+ return res ;
19
+ }
You can’t perform that action at this time.
0 commit comments