Leetcode 3. Longest Substring Without Repeating Characters

smpl published on
2 min, 212 words

문제 : 3. Longest Substring Without Repeating Characters

등급 : Medium

#![allow(dead_code)]
pub(self) mod structural_try {
    // 처음 짜본 버전..

    use std::collections::VecDeque;
    use std::cmp::PartialEq;

    pub struct Solution;
    impl Solution {
        fn find<T: std::cmp::PartialEq + Copy>(vd: &VecDeque<T>, fv: &T) -> Option<usize> {
            for (i, v) in vd.iter().enumerate() {
                if v == fv {
                    return Some(i)
                }
            }

            None
        }

        pub fn length_of_longest_substring(s: String) -> i32 {
            let mut subchars = VecDeque::new();
            let mut longest = 0i32;

            for c in s.chars() {
                if let Some(pos) = Self::find::<char>(&subchars, &c) {
                    subchars.drain(0..pos+1);
                }

                subchars.push_back(c);

                if longest < subchars.len() as i32 {
                    longest = subchars.len() as i32;
                }
            }

            return longest;
        }
    }
}

#[allow(dead_code)]
pub(self) mod functional_try {
    // 조금이라도 더 함수형으로 짜려고 시도한 버전

    use std::collections::VecDeque;
    use std::cmp::max;

    pub struct Solution;
    impl Solution {
        pub fn length_of_longest_substring(s: String) -> i32 {
            s.chars().fold((VecDeque::new(), 0), |(mut subchars, mut longest), c| {
                if let Some(i) = subchars.iter().position(|v| v == &c) {
                    subchars.drain(0..i+1);
                }

                subchars.push_back(c);

                longest = max(longest, subchars.len());

                (subchars, longest)
            }).1 as i32
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::longest_substring_without_repeating_characters::functional_try::Solution;

    #[test]
    fn length_of_longest_substring() {
        assert_eq!(Solution::length_of_longest_substring("abcabcbb".to_owned()), 3);
        assert_eq!(Solution::length_of_longest_substring("bbbbb".to_owned()), 1);
        assert_eq!(Solution::length_of_longest_substring("pwwkew".to_owned()), 3);
        assert_eq!(Solution::length_of_longest_substring("".to_owned()), 0);
    }
}