8000 longest repeating char · jhideki/rust_leetcode@b388fc4 · GitHub
[go: up one dir, main page]

Skip to content

Commit b388fc4

Browse files
committed
longest repeating char
1 parent f08a2db commit b388fc4

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

src/longest_repeating_character.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use std::{cmp::max, collections::HashMap};
2+
pub struct Solution {}
3+
impl Solution {
4+
pub fn character_replacement(s: String, k: i32) -> i32 {
5+
let mut result: i32 = 0;
6+
let mut lp: i32 = 0;
7+
let mut map: HashMap<char, i32> = HashMap::new();
8+
let chars: Vec<char> = s.chars().collect();
9+
let mut max_f = 0;
10+
for rp in 0..chars.len() {
11+
let val = map
12+
.entry(chars[rp as usize])
13+
.and_modify(|e| *e += 1)
14+
.or_insert(1);
15+
max_f = max(val.clone(), max_f);
16+
17+
while (rp as i32 - lp + 1) - max_f > k {
18+
map.entry(chars[lp as usize]).and_modify(|e| *e -= 1);
19+
lp += 1;
20+
}
21+
result = max(result, rp as i32 - lp + 1);
22+
}
23+
24+
result
25+
}
26+
}

src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
mod longest_substring;
2-
use longest_substring::Solution;
1+
mod longest_repeating_character;
2+
use longest_repeating_character::Solution;
33
fn main() {
4-
let s = "au".to_string();
5-
let result = Solution::length_of_longest_substring(s);
4+
let s = "AABABBBBB".to_string();
5+
let result = Solution::character_replacement(s, 0);
66
print!("{:?}", result);
77
}

0 commit comments

Comments
 (0)
0