File tree 3 files changed +48
-2
lines changed
solution/2200-2299/2287.Rearrange Characters to Make Target String
3 files changed +48
-2
lines changed Original file line number Diff line number Diff line change @@ -162,7 +162,22 @@ func min(a, b int) int {
162
162
### ** TypeScript**
163
163
164
164
``` ts
165
-
165
+ function rearrangeCharacters(s : string , target : string ): number {
166
+ let cnt1 = new Array (128 ).fill (0 ),
167
+ cnt2 = new Array (128 ).fill (0 );
168
+ for (let i of target ) {
169
+ cnt1 [i .charCodeAt (0 )]++ ;
170
+ }
171
+ for (let i of s ) {
172
+ cnt2 [i .charCodeAt (0 )]++ ;
173
+ }
174
+ let ans = Infinity ;
175
+ for (let i = 0 ; i < 128 ; i ++ ) {
176
+ if (cnt1 [i ] === 0 ) continue ;
177
+ ans = Math .min (ans , Math .floor (cnt2 [i ] / cnt1 [i ]));
178
+ }
179
+ return ans === Infinity ? 0 : ans ;
180
+ };
166
181
```
167
182
168
183
### ** ...**
Original file line number Diff line number Diff line change @@ -155,7 +155,22 @@ func min(a, b int) int {
155
155
### ** TypeScript**
156
156
157
157
``` ts
158
-
158
+ function rearrangeCharacters(s : string , target : string ): number {
159
+ let cnt1 = new Array (128 ).fill (0 ),
160
+ cnt2 = new Array (128 ).fill (0 );
161
+ for (let i of target ) {
162
+ cnt1 [i .charCodeAt (0 )]++ ;
163
+ }
164
+ for (let i of s ) {
165
+ cnt2 [i .charCodeAt (0 )]++ ;
166
+ }
167
+ let ans = Infinity ;
168
+ for (let i = 0 ; i < 128 ; i ++ ) {
169
+ if (cnt1 [i ] === 0 ) continue ;
170
+ ans = Math .min (ans , Math .floor (cnt2 [i ] / cnt1 [i ]));
171
+ }
172
+ return ans === Infinity ? 0 : ans ;
173
+ };
159
174
```
160
175
161
176
### ** ...**
Original file line number Diff line number Diff line change
1
+ function rearrangeCharacters ( s : string , target : string ) : number {
2
+ let cnt1 = new Array ( 128 ) . fill ( 0 ) ,
3
+ cnt2 = new Array ( 128 ) . fill ( 0 ) ;
4
+ for ( let i of target ) {
5
+ cnt1 [ i . charCodeAt ( 0 ) ] ++ ;
6
+ }
7
+ for ( let i of s ) {
8
+ cnt2 [ i . charCodeAt ( 0 ) ] ++ ;
9
+ }
10
+ let ans = Infinity ;
11
+ for ( let i = 0 ; i < 128 ; i ++ ) {
12
+ if ( cnt1 [ i ] === 0 ) continue ;
13
+ ans = Math . min ( ans , Math . floor ( cnt2 [ i ] / cnt1 [ i ] ) ) ;
14
+ }
15
+ return ans === Infinity ? 0 : ans ;
16
+ } ;
You can’t perform that action at this time.
0 commit comments