pub fn max_substring(s: &str) -> (usize, usize, usize) {
let bytes = s.as_bytes();
let mut char_set = [false; 128];
let (mut max_left, mut max_right, mut max_length) = (0, 0, 0); let mut left = 0;
for right in 0..bytes.len() {
while char_set[bytes[right] as usize] == true {
char_set[bytes[left] as usize] = false; left += 1; }
char_set[bytes[right] as usize] = true;
if right - left + 1 > max_length {
max_length = right - left + 1;
max_left = left; max_right = right; }
}
return (max_left, max_right, max_length);
}
#[cfg(test)]
mod max_substring {
use super::*;
#[test]
fn test_empty_string() {
assert_eq!(max_substring(""), (0, 0, 0));
}
#[test]
fn test_single_character_string() {
assert_eq!(max_substring("a"), (0, 0, 1));
}
#[test]
fn two_same_characters() {
assert_eq!(max_substring("aa"), (0, 0, 1));
}
#[test]
fn test_all_same_characters() {
assert_eq!(max_substring("aaaaa"), (0, 0, 1));
}
#[test]
fn test_no_repeating_characters() {
assert_eq!(max_substring("abcde"), (0, 4, 5));
}
#[test]
fn test_repeating_characters() {
assert_eq!(max_substring("abcabcbb"), (0, 2, 3));
}
#[test]
fn test_mixed_characters() {
assert_eq!(max_substring("pwwkew"), (2, 4, 3));
}
#[test]
fn test_with_numbers() {
assert_eq!(max_substring("123456789012345678901234567890"), (0, 9, 10));
}
#[test]
fn test_with_special_characters() {
assert_eq!(max_substring("!@#$%^&*(*)_+"), (0, 8, 9));
}
#[test]
fn test_long_string_with_repeating_characters() {
assert_eq!(max_substring("dvdf"), (1, 3, 3));
}
#[test]
fn test_string_with_repeating_characters_at_end() {
assert_eq!(max_substring("abcdefgg"), (0, 6, 7));
}
#[test]
fn test_string_with_repeating_characters_at_start() {
assert_eq!(max_substring("bbbbb"), (0, 0, 1));
}
}