We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f58e23d commit a44e9d7Copy full SHA for a44e9d7
1 file changed
longest-repeating-character-replacement/DaleSeo.rs
@@ -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