File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed
rearrange-characters-to-make-target-string Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change
1
+ function rearrangeCharacters ( s : string , target : string ) : number {
2
+
3
+ const sCounts = new Map < string , number > ( ) ;
4
+ const targetCounts = new Map < string , number > ( ) ;
5
+ const n = s . length , m = target . length ;
6
+ for ( let i = 0 ; i < m ; i ++ ) {
7
+ const c = target [ i ] ;
8
+ targetCounts . set ( c , ( targetCounts . get ( c ) || 0 ) + 1 ) ;
9
+ }
10
+ for ( let i = 0 ; i < n ; i ++ ) {
11
+ const c = s [ i ] ;
12
+ if ( targetCounts . has ( c ) ) {
13
+ sCounts . set ( c , ( sCounts . get ( c ) || 0 ) + 1 ) ;
14
+ }
15
+ }
16
+ let ans = Number . MAX_VALUE ;
17
+ for ( const [ c , count ] of targetCounts ) {
18
+ const totalCount = ( sCounts . has ( c ) ? sCounts . get ( c ) : 0 ) ?? 0 ;
19
+ ans = Math . min ( ans , Math . floor ( totalCount / count ) ) ;
20
+ if ( ans === 0 ) {
21
+ return 0 ;
22
+ }
23
+ }
24
+ return ans ;
25
+ }
26
+ export default rearrangeCharacters
You can’t perform that action at this time.
0 commit comments