leetcode/
longest_substring_without_repeating_characters.rs1pub struct Solution;
37
38impl Solution {
39 pub fn length_of_longest_substring(s: String) -> i32 {
40 Self::impl_sliding_window(&s)
41 }
42
43 fn impl_sliding_window(s: &str) -> i32 {
44 let last_index = std::collections::HashMap::new();
45 let start = 0;
46 let max_length = 0;
47 let init = (max_length, start, last_index);
48 s.char_indices()
49 .fold(init, |(max_length, last_start, mut last_index), (idx, c)| {
50 let new_start = last_index.insert(c, idx).map_or(last_start, |pos| last_start.max(pos + 1));
51 (max_length.max(idx - new_start + 1), new_start, last_index)
52 })
53 .0 as i32
54 }
55}
56
57#[cfg(test)]
58mod tests {
59 #[test]
60 fn test() {
61 use super::Solution;
62 assert_eq!(3, Solution::length_of_longest_substring("abcabcbb".to_string()));
63 assert_eq!(1, Solution::length_of_longest_substring("bbbbb".to_string()));
64 assert_eq!(3, Solution::length_of_longest_substring("pwwkew".to_string()));
65 assert_eq!(1, Solution::length_of_longest_substring(" ".to_string()));
66 assert_eq!(6, Solution::length_of_longest_substring("bbtablud".to_string()));
67 assert_eq!(3, Solution::length_of_longest_substring("aabaab!bb".to_string()));
68 }
69}