Skip to content

Commit 4be49a1

Browse files
committed
✨ [424] Longest Repeating Character Replacement
1 parent e230637 commit 4be49a1

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

424/my_solution.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
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);
24+
}
25+
}
26+
27+
return max;
28+
}

424/solution.js

Whitespace-only changes.

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
- [371. Sum of Two Integers](./371/)
4646
- [338. Counting Bits](./338/)
4747
- [394. Decode String](./394/)
48+
- [424. Longest Repeating Character Replacement](./424/)
4849

4950
---
5051

@@ -78,5 +79,5 @@ Batch create in bash
7879
TODO: Add to TOC!
7980
-->
8081
```ssh
81-
chapter=49 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
82+
chapter=424 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
8283
```

0 commit comments

Comments
 (0)