Skip to content

Commit 2878106

Browse files
authored
Update 1737-change-minimum-characters-to-satisfy-one-of-three-conditions.js
1 parent be31536 commit 2878106

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

1737-change-minimum-characters-to-satisfy-one-of-three-conditions.js

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,28 @@
33
* @param {string} b
44
* @return {number}
55
*/
6-
const minCharacters = function(a, b) {
7-
let n1 = a.length, n2 = b.length;
8-
const cnt1 = Array(26).fill(0);
9-
const cnt2 = Array(26).fill(0);
10-
const aCode = ('a').charCodeAt(0);
11-
for (let c of a) ++cnt1[c.charCodeAt(0) - aCode];
12-
for (let c of b) ++cnt2[c.charCodeAt(0) - aCode];
13-
let res = n1 - Math.max(...cnt1) + n2 - Math.max(...cnt2);
14-
for (let i = 0; i < 25; ++i) {
15-
let cur1 = 0, cur2 = 0;
16-
for (let j = 0; j < 26; ++j) {
17-
if (j <= i) {
18-
cur1 += cnt2[j];
19-
cur2 += cnt1[j];
20-
} else {
21-
cur1 += cnt1[j];
22-
cur2 += cnt2[j];
23-
}
6+
const minCharacters = function (a, b) {
7+
const n1 = a.length,
8+
n2 = b.length
9+
const cnt1 = Array(26).fill(0)
10+
const cnt2 = Array(26).fill(0)
11+
const aCode = 'a'.charCodeAt(0)
12+
for (let c of a) ++cnt1[c.charCodeAt(0) - aCode]
13+
for (let c of b) ++cnt2[c.charCodeAt(0) - aCode]
14+
let res = n1 - Math.max(...cnt1) + n2 - Math.max(...cnt2)
15+
for (let i = 0; i < 25; ++i) {
16+
let cur1 = 0,
17+
cur2 = 0
18+
for (let j = 0; j < 26; ++j) {
19+
if (j <= i) {
20+
cur1 += cnt2[j]
21+
cur2 += cnt1[j]
22+
} else {
23+
cur1 += cnt1[j]
24+
cur2 += cnt2[j]
2425
}
25-
res = Math.min(Math.min(cur1, cur2), res);
2626
}
27-
return res;
28-
};
27+
res = Math.min(Math.min(cur1, cur2), res)
28+
}
29+
return res
30+
}

0 commit comments

Comments
 (0)