Skip to content

Commit 85110e9

Browse files
authored
2 parents 3088d12 + aa1c42a commit 85110e9

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ Step 2. Add the dependency
4949

5050
<summary>展开查看</summary>
5151

52+
https://leetcode-cn.com/problems/rearrange-characters-to-make-target-string/
53+
5254
https://leetcode-cn.com/problems/assign-cookies/
5355

5456
https://leetcode-cn.com/problems/evaluate-the-bracket-pairs-of-a-string/
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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

0 commit comments

Comments
 (0)