Skip to content

Commit a44e9d7

Browse files
committed
longest-repeating-character-replacement
1 parent f58e23d commit a44e9d7

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// TC: O(n), where n is s.len()
2+
// SC: O(1), fixed 26-slot frequency array
3+
impl Solution {
4+
pub fn character_replacement(s: String, k: i32) -> i32 {
5+
let bytes = s.as_bytes();
6+
let mut counts = [0i32; 26];
7+
let mut start = 0usize;
8+
let mut max_freq = 0i32;
9+
let mut result = 0i32;
10+
11+
for end in 0..bytes.len() {
12+
let idx = (bytes[end] - b'A') as usize;
13+
counts[idx] += 1;
14+
max_freq = max_freq.max(counts[idx]);
15+
16+
while (end - start + 1) as i32 - max_freq > k {
17+
let left_idx = (bytes[start] - b'A') as usize;
18+
counts[left_idx] -= 1;
19+
start += 1;
20+
}
21+
22+
result = result.max((end - start + 1) as i32);
23+
}
24+
25+
result
26+
}
27+
}

0 commit comments

Comments
 (0)