From afabed5d35927a5de9a699daa8d50c6a834e8d7a Mon Sep 17 00:00:00 2001 From: Gleb Koval Date: Fri, 10 Jun 2022 11:55:17 +0100 Subject: [PATCH] longest-substring-without-repeating-characters --- .../sol.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 longest-substring-without-repeating-characters/sol.rs diff --git a/longest-substring-without-repeating-characters/sol.rs b/longest-substring-without-repeating-characters/sol.rs new file mode 100644 index 0000000..0570ae1 --- /dev/null +++ b/longest-substring-without-repeating-characters/sol.rs @@ -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 + } +} \ No newline at end of file