Skip to content

Commit 92d492f

Browse files
authored
Create 2287-rearrange-characters-to-make-target-string.js
1 parent 881ef40 commit 92d492f

File tree

1 file changed

+26
-0
lines changed

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+
/**
2+
* @param {string} s
3+
* @param {string} target
4+
* @return {number}
5+
*/
6+
var rearrangeCharacters = function(s, target) {
7+
const a = 'a'.charCodeAt(0), arr = Array(26).fill(0)
8+
for(let ch of target) {
9+
arr[ch.charCodeAt(0) - a]++
10+
}
11+
let min = Math.min(...arr.filter(e => e > 0))
12+
const sa = Array(26).fill(0)
13+
for(const e of s) {
14+
sa[e.charCodeAt(0) - a]++
15+
}
16+
let res = -1
17+
for(let i = 0; i < 26; i++) {
18+
const sv = sa[i], tv = arr[i]
19+
if(tv === 0) continue
20+
const v = ~~(sv / tv)
21+
if(res === -1) res = v
22+
else res = Math.min(res, v)
23+
}
24+
25+
return res
26+
};

0 commit comments

Comments
 (0)