longest-substring-without-repeating-characters

This commit is contained in:
Gleb Koval 2022-06-10 11:55:17 +01:00
parent a01ec4f2ac
commit afabed5d35
Signed by: cyclane
GPG Key ID: 15E168A8B332382C
1 changed files with 18 additions and 0 deletions

View File

@ -0,0 +1,18 @@
use std::collections::HashSet;
impl Solution {
pub fn length_of_longest_substring(s: String) -> i32 {
let bytes = s.as_bytes();
let mut exists = HashSet::new();
let mut max = 0;
let mut p1 = 0;
for p2 in 0..s.len() {
while !exists.insert(bytes[p2]) {
exists.remove(&bytes[p1]);
p1 += 1;
}
max = max.max(exists.len());
}
max as i32
}
}