Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ea9a02c

Browse files
feat: add typescript solution to lc problem: No.2287
No.2287.Rearrange Characters to Make Target String
1 parent a80b022 commit ea9a02c

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

‎solution/2200-2299/2287.Rearrange Characters to Make Target String/README.md‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,22 @@ func min(a, b int) int {
162162
### **TypeScript**
163163

164164
```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+
};
166181
```
167182

168183
### **...**

‎solution/2200-2299/2287.Rearrange Characters to Make Target String/README_EN.md‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,22 @@ func min(a, b int) int {
155155
### **TypeScript**
156156

157157
```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+
};
159174
```
160175

161176
### **...**
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /