Loading RustViz Engine...
Find the length of the longest substring without duplicate characters using sliding window.
This problem is currently in blueprint template mode. Once the concept foundation is finalized, live in-browser WebAssembly compilation and test assertion runners will be enabled here.
use std::collections::HashMap;
pub fn length_of_longest_substring(s: &str) -> usize {
let mut map = HashMap::new();
let (mut max_len, mut start) = (0, 0);
for (i, c) in s.chars().enumerate() {
if let Some(&prev) = map.get(&c) {
if prev >= start {
start = prev + 1;
}
}
map.insert(c, i);
max_len = max_len.max(i - start + 1);
}
max_len
}