From 287d3a79146fa2a76d150dbb56ee51796f32b15e Mon Sep 17 00:00:00 2001 From: Myeongseon Choi Date: Mon, 23 Sep 2024 16:19:53 +0900 Subject: [PATCH] id 1399197705 Time: 136 ms MemUsage: 2.1 MB --- .../solution_1399197705.rs | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 leetcode/longest-substring-without-repeating-characters/solution_1399197705.rs diff --git a/leetcode/longest-substring-without-repeating-characters/solution_1399197705.rs b/leetcode/longest-substring-without-repeating-characters/solution_1399197705.rs new file mode 100644 index 0000000..8501aa0 --- /dev/null +++ b/leetcode/longest-substring-without-repeating-characters/solution_1399197705.rs @@ -0,0 +1,33 @@ +use std::ops::ControlFlow; +use std::collections::HashSet; + +impl Solution { + pub fn length_of_longest_substring(s: String) -> i32 { + let string_length = s.len(); + + match (0..string_length).try_fold(0, |max_length, i| { + match max_length < string_length - i { + true => { + match s[i..].bytes().try_fold((0, 0, HashSet::new()), |(max_length, length, mut counter), x| { + match (counter.contains(&x), length > max_length){ + (true, true) => ControlFlow::Break(length), + (true, false) => ControlFlow::Break(max_length), + (false, _) => { + counter.insert(x); + ControlFlow::Continue((max_length, length + 1, counter)) + }, + } + }){ + ControlFlow::Break(length) => ControlFlow::Continue(max_length.max(length)), + ControlFlow::Continue((ml, l, _)) => ControlFlow::Continue(max_length.max(ml.max(l))), + } + }, + false => ControlFlow::Break(max_length), + } + }){ + ControlFlow::Continue(length) => length as i32, + ControlFlow::Break(length) => length as i32, + _ => unreachable!() + } + } +} \ No newline at end of file