|
| 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 | +// } |
1 | 41 | 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++; |
24 | 58 | }
|
| 59 | + |
| 60 | + max = Math.max(max, (r - l + 1)); |
| 61 | + |
| 62 | + r++; |
25 | 63 | }
|
26 | 64 |
|
| 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 | + |
27 | 72 | return max;
|
28 | 73 | }
|
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +// characterReplacement("ABAB", 2) |
| 78 | +// characterReplacement("AABABBA", 1) |
| 79 | +// characterReplacement("AAAA", 2) |
| 80 | +characterReplacement("ABBB", 2) |
| 81 | +// characterReplacement("AABABBA", 1) |
0 commit comments