Skip to content

Commit aa1c42a

Browse files
authored
Create index.ts
1 parent dc6deb4 commit aa1c42a

File tree

1 file changed

+26
-0
lines changed
  • rearrange-characters-to-make-target-string

1 file changed

+26
-0
lines changed
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)