Skip to content

Commit 46b1f6d

Browse files
committed
✨ [424] NeetCode's idea
Runtime 68 ms Beats 93.96% Memory 44.2 MB Beats 35.78%
1 parent 4be49a1 commit 46b1f6d

File tree

2 files changed

+100
-22
lines changed

2 files changed

+100
-22
lines changed

424/my_solution.js

+75-22
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,81 @@
1+
// const characterReplacement = (s, k) => {
2+
// let max = 0, char = s.split("");
3+
//
4+
// // loop thru the char, to move expand it by k if NOT the same (then decrease the tmpK)
5+
//
6+
//
7+
// for (let i = 0; i < char.length; i++) {
8+
// let count = 1, tmpK = k;
9+
//
10+
// console.log("-----> " + i)
11+
// for (let j = 0; j < char.length; j++) {
12+
// if (i === j) continue;
13+
// console.log(`1:${char[i]}, 2:${char[j]}, tmpK:${tmpK}`)
14+
//
15+
// if (char[i] !== char[j]) {
16+
// if (tmpK > 0) {
17+
// // increase count by 1 when same or k > 0 & not the same
18+
// tmpK--;
19+
// count++;
20+
// console.log(`---> char:${char[i]}, tmpK:${tmpK}, count:${count}`)
21+
// } else { // stop when diff and k == 0
22+
// count = 1;
23+
// console.log("-> stop:" + count)
24+
// continue;
25+
// }
26+
// } else {
27+
// count++;
28+
// console.log(`${char[i]}==${char[j]}, tmpK:${tmpK}, count:${count}`)
29+
// }
30+
//
31+
// // get max from count and max
32+
// max = Math.max(max, count);
33+
// }
34+
// }
35+
//
36+
// console.log("max")
37+
// console.log(max)
38+
//
39+
// return max;
40+
// }
141
const characterReplacement = (s, k) => {
2-
let max = 0, char = s.split("");
3-
4-
// loop thru the char, to move expand it by k if NOT the same (then decrease the tmpK)
5-
6-
for (let i = 0; i < char.length; i++) {
7-
for (let j = i + 1; j < char.length; j++) {
8-
let count = 0, tmpK = k;
9-
// clean up
10-
if (char[i] !== char[j]) {
11-
if (tmpK > 0) {
12-
// increase count by 1 when same or k > 0 & not the same
13-
tmpK--;
14-
count++;
15-
} else { // stop when diff and k == 0
16-
continue;
17-
}
18-
} else {
19-
count++;
20-
}
21-
22-
// get max from count and max
23-
max = Math.max(max, count);
42+
let max = 0, maxF = 0, char = s.split(""), map = new Map(), l = 0, r = 0;
43+
44+
while (r < char.length) {
45+
let newC = (map.get(char[r]) || 0) + 1;
46+
console.log(char[r] + ":newC:" + newC)
47+
map.set(char[r], newC)
48+
49+
maxF = Math.max(maxF, newC);
50+
51+
// console.log(((r - l + 1) - maxF))
52+
if (((r - l + 1) - maxF) > k) {
53+
console.log("reset")
54+
console.log(map.get(char[l]))
55+
map.set(char[l], map.get(char[l]) - 1);
56+
console.log(map.get(char[l]))
57+
l++;
2458
}
59+
60+
max = Math.max(max, (r - l + 1));
61+
62+
r++;
2563
}
2664

65+
console.log("CHECK")
66+
console.log(max)
67+
// console.log(Array.from(map.keys()).length)
68+
// console.log(maxF)
69+
// console.log(k)
70+
// console.log(maxF + ((1 === Array.from(map.keys()).length) ? 0 : k))
71+
2772
return max;
2873
}
74+
75+
76+
77+
// characterReplacement("ABAB", 2)
78+
// characterReplacement("AABABBA", 1)
79+
// characterReplacement("AAAA", 2)
80+
characterReplacement("ABBB", 2)
81+
// characterReplacement("AABABBA", 1)

424/solution.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {string} s
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
const characterReplacement = (s, k) => {
7+
let max = 0, maxF = 0, char = s.split(""), map = new Map(), l = 0, r = 0;
8+
9+
while (r < char.length) {
10+
let newC = (map.get(char[r]) || 0) + 1;
11+
map.set(char[r], newC)
12+
maxF = Math.max(maxF, newC);
13+
14+
if (((r - l + 1) - maxF) > k) {
15+
map.set(char[l], map.get(char[l]) - 1);
16+
l++;
17+
}
18+
19+
max = Math.max(max, (r - l + 1));
20+
21+
r++;
22+
}
23+
24+
return max;
25+
}

0 commit comments

Comments
 (0)