Skip to content

Commit c73bd79

Browse files
committed
2287. 重排字符形成目标字符串
1 parent d2fa2f8 commit c73bd79

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@
178178
|1945|[字符串转化后的各位数字之和](https://leetcode.cn/problems/sum-of-digits-of-string-after-convert/)|[JavaScript](./algorithms/sum-of-digits-of-string-after-convert.js)|Easy|
179179
|2032|[至少在两个数组中出现的值](https://leetcode.cn/problems/two-out-of-three/)|[JavaScript](./algorithms/two-out-of-three.js)|Easy|
180180
|2283|[判断一个数的数字计数是否等于数位的值](https://leetcode.cn/problems/check-if-number-has-equal-digit-count-and-digit-value/)|[JavaScript](./algorithms/check-if-number-has-equal-digit-count-and-digit-value.js)|Easy|
181+
|2287|[重排字符形成目标字符串](https://leetcode.cn/problems/rearrange-characters-to-make-target-string/)|[JavaScript](./algorithms/rearrange-characters-to-make-target-string.js)|Easy|
181182
|面试题 04.12|[面试题 04.12. 求和路径](https://leetcode.cn/problems/paths-with-sum-lcci/)|[JavaScript](./algorithms/paths-with-sum-lcci.js)|Medium|
182183
|面试题 02.07|[面试题 02.07. 链表相交](https://leetcode.cn/problems/intersection-of-two-linked-lists-lcci/)|[JavaScript](./algorithms/intersection-of-two-linked-lists-lcci.js)|Easy|
183184
|剑指 Offer 05. 替换空格|[剑指 Offer 05. 替换空格](https://leetcode.cn/problems/ti-huan-kong-ge-lcof/)|[JavaScript](./algorithms/ti-huan-kong-ge-lcof.js)|Easy|
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* [2287] 重排字符形成目标字符串
3+
* @param {string} s
4+
* @param {string} target
5+
* @return {number}
6+
*/
7+
var rearrangeCharacters = function (s, target) {
8+
// 思路: 分别统计 s target 中每个字符出现的次数,然后求最小倍数
9+
10+
let result = Number.MAX_VALUE;
11+
const map1 = Array(26).fill(0);
12+
const map2 = Array(26).fill(0);
13+
for (let i = 0; i < s.length; i++) {
14+
const index = s[i].charCodeAt() - 97;
15+
map1[index]++;
16+
}
17+
for (let i = 0; i < target.length; i++) {
18+
const index = target[i].charCodeAt() - 97;
19+
map2[index]++;
20+
}
21+
for (let i = 0; i < map1.length; i++) {
22+
const a = map1[i];
23+
const b = map2[i];
24+
const c = b ? Math.floor(a / b) : 0;
25+
26+
if (!a && b) {
27+
return 0;
28+
}
29+
if (a && b) {
30+
result = Math.min(result, c);
31+
}
32+
}
33+
return result;
34+
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* @lc app=leetcode.cn id=2287 lang=javascript
3+
*
4+
* [2287] 重排字符形成目标字符串
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* @param {string} s
10+
* @param {string} target
11+
* @return {number}
12+
*/
13+
var rearrangeCharacters = function(s, target) {
14+
// 思路: 分别统计 s target 中每个字符出现的次数,然后求最小倍数
15+
16+
let result = Number.MAX_VALUE;
17+
const map1 = Array(26).fill(0);
18+
const map2 = Array(26).fill(0);
19+
for (let i = 0; i < s.length; i++) {
20+
const index = s[i].charCodeAt() - 97;
21+
map1[index]++;
22+
}
23+
for (let i = 0; i < target.length; i++) {
24+
const index = target[i].charCodeAt() - 97;
25+
map2[index]++;
26+
}
27+
for (let i = 0; i < map1.length; i++) {
28+
const a = map1[i];
29+
const b = map2[i];
30+
const c = b ? Math.floor(a / b) : 0;
31+
32+
if (!a && b) {
33+
return 0;
34+
}
35+
if (a && b) {
36+
result = Math.min(result, c);
37+
}
38+
}
39+
return result;
40+
};
41+
// @lc code=end
42+

0 commit comments

Comments
 (0)