File tree Expand file tree Collapse file tree 2 files changed +28
-0
lines changed
rearrange-characters-to-make-target-string Expand file tree Collapse file tree 2 files changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -49,6 +49,8 @@ Step 2. Add the dependency
49
49
50
50
<summary >展开查看</summary >
51
51
52
+ https://leetcode-cn.com/problems/rearrange-characters-to-make-target-string/
53
+
52
54
https://leetcode-cn.com/problems/assign-cookies/
53
55
54
56
https://leetcode-cn.com/problems/evaluate-the-bracket-pairs-of-a-string/
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