Skip to content

Commit 24f4a4d

Browse files
committed
feat: Add solution for LeetCode problem 424
1 parent a142303 commit 24f4a4d

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// 424. Longest Repeating Character Replacement
3+
// https://leetcode.com/problems/longest-repeating-character-replacement/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/06/08.
7+
//
8+
9+
final class Solution {
10+
func characterReplacement(_ s: String, _ k: Int) -> Int {
11+
var maxFrequency = 0
12+
var i = 0
13+
var counter: [Character: Int] = [:]
14+
let array = Array(s)
15+
16+
for index in array.indices {
17+
counter[array[index], default: 0] += 1
18+
if maxFrequency < counter[array[index]]! {
19+
maxFrequency = counter[array[index]]!
20+
}
21+
22+
if index - i + 1 > maxFrequency + k {
23+
counter[array[i]]! -= 1
24+
i += 1
25+
}
26+
}
27+
28+
return array.count - i
29+
}
30+
}

0 commit comments

Comments
 (0)