1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| use std::collections::HashSet;
impl Solution { pub fn length_of_longest_substring(s: String) -> i32 { let mut occ: HashSet<char> = HashSet::new(); let cv: Vec<char> = s.chars().collect(); let n = s.len(); let mut rk = 0; let mut ans = 0; for i in 0..n{ if i != 0 { occ.remove(&cv[i-1]); } while rk < n && !occ.contains(&cv[rk]) { occ.insert(cv[rk]); rk += 1; } ans = ans.max(rk - i); } ans as i32 } }
|