We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 9a8f8ba commit 88ea0f2Copy full SHA for 88ea0f2
javascript/0424-longest-repeating-character-replacement.js
@@ -1,3 +1,30 @@
1
+/**
2
+ * https://leetcode.com/problems/longest-repeating-character-replacement/
3
+ * Time O(((N + 26) * N) * (M - N)) | Space O(1)
4
+ * @param {string} s
5
+ * @param {number} k
6
+ * @return {number}
7
+ */
8
+var characterReplacement = function(s, k) {
9
+ let res = 0;
10
+ let count = new Map();
11
+ let l = 0;
12
+
13
+ for (let r = 0; r < s.length; r++) {
14
+ let len = r - l + 1
15
+ count.set(s[r], 1 + (count.get(s[r]) || 0))
16
17
+ if ((len - Math.max(...count.values())) > k) {
18
+ count.set(s[l], count.get(s[l]) - 1)
19
+ l++;
20
+ }
21
+ len = r - l + 1;
22
+ res = Math.max(res, len)
23
24
25
+ return res;
26
+};
27
28
/**
29
* https://leetcode.com/problems/longest-repeating-character-replacement/
30
* Time O(((N + 26) * N) * (M - N)) | Space O(1)
0 commit comments