-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrepeat.rs
50 lines (45 loc) · 1.45 KB
/
repeat.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use std::collections::HashMap;
/**
* Write a program to find the longest length of a substring without repetition
* of a character more than 2 times.
*
* example: a string "abcdeaabcdef" contains 3 'a's but if you take a substring
* from b then there will be no characters that gets repeated more than twice.
* The length of the substring will then be 11.
*/
fn longest_substring_with_less_repeat(data: String) -> usize {
let mut substring_lengths: Vec<usize> = vec![];
for start in 0..data.len() {
let slice = data[start..data.len()].to_owned();
let mut map: HashMap<u8, usize> = HashMap::new();
let mut end = start;
for &ch in slice.as_bytes() {
end += 1;
match map.get(&ch) {
Some(&size) => {
if size == 2 {
end -= 1;
break;
} else {
map.insert(ch, size + 1);
}
}
None => {
map.insert(ch, 1);
}
}
}
substring_lengths.push(end - start);
}
substring_lengths.iter().max().unwrap().to_owned()
}
fn main() {
println!(
"{}",
longest_substring_with_less_repeat("abcdeaabcdef".to_owned()), // 11 [bcdeaabcdef]
);
println!(
"{}",
longest_substring_with_less_repeat("abcdeaabcdaef".to_owned()), // 9 [bcdeaabcd]
);
}