Skip to content

Commit 5741d55

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

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

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

+52
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,55 @@
1+
/**
2+
* @param {string} a
3+
* @param {string} b
4+
* @return {number}
5+
*/
6+
const minCharacters = function(a, b) {
7+
const n = a.length, m = b.length;
8+
const freqA = Array(26).fill(0), freqB = Array(26).fill(0);
9+
const ac = 'a'.charCodeAt(0)
10+
for(let i = 0; i < n; i++) {
11+
freqA[a.charCodeAt(i) - ac]++
12+
}
13+
for(let i = 0; i < m; i++) {
14+
freqB[b.charCodeAt(i) - ac]++
15+
}
16+
let res = Infinity
17+
for(let i = 0; i < 26; i++) {
18+
if(i > 0) {
19+
let change = 0
20+
for(let j = 0; j < i; j++) {
21+
change += freqA[j]
22+
}
23+
for(let j = i; j < 26; j++) {
24+
change += freqB[j]
25+
}
26+
res = Math.min(res, change)
27+
change = 0
28+
for(let j = 0; j < i; j++) {
29+
change += freqB[j]
30+
}
31+
for(let j = i; j < 26; j++) {
32+
change += freqA[j]
33+
}
34+
res = Math.min(res, change)
35+
}
36+
let change = 0
37+
for(let j = 0; j < 26; j++) {
38+
if(j !== i) {
39+
change += freqA[j]
40+
change += freqB[j]
41+
}
42+
}
43+
res = Math.min(res, change)
44+
}
45+
46+
return res
47+
};
48+
49+
50+
// another
51+
52+
153
/**
254
* @param {string} a
355
* @param {string} b

0 commit comments

Comments
 (0)