id 1399197705 Time: 136 ms MemUsage: 2.1 MB

This commit is contained in:
2024-09-23 16:19:53 +09:00
parent 1a145853b1
commit 287d3a7914

View File

@@ -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!()
}
}
}