diff options
| author | m4siri <git@m4siri.com> | 2025-11-30 20:47:11 +0545 |
|---|---|---|
| committer | m4siri <git@m4siri.com> | 2025-11-30 20:50:52 +0545 |
| commit | a5e3b1140ec34399e172e0405eb99f323e275a24 (patch) | |
| tree | d3b436ed072a86e53d3fae7a2ebdf35a7337f19f /03_longest-substring-without-repeating-chars/src/main.rs | |
lc starting today - 2025-11-30
Diffstat (limited to '03_longest-substring-without-repeating-chars/src/main.rs')
| -rw-r--r-- | 03_longest-substring-without-repeating-chars/src/main.rs | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/03_longest-substring-without-repeating-chars/src/main.rs b/03_longest-substring-without-repeating-chars/src/main.rs new file mode 100644 index 0000000..f07bedf --- /dev/null +++ b/03_longest-substring-without-repeating-chars/src/main.rs @@ -0,0 +1,46 @@ +use std::collections::HashSet; + +fn main() { + let s = "dvdf".to_string(); + dbg!(Solution::length_of_longest_substring(s)); +} + +struct Solution(()); + +impl Solution { + pub fn length_of_longest_substring(s: String) -> i32 { + use std::collections::HashMap; + + let s = s.chars().collect::<Vec<char>>(); + let mut max_length = 0; + let mut length = 0; + let mut map: HashMap<char, i32> = HashMap::new(); + let mut idx = 0; + + while idx < s.len() { + let ch = &s[idx]; + + if let Some(ref ci) = map.remove(ch) { + map = HashMap::new(); + if max_length < length { + max_length = length; + } + + idx = (ci + 1) as usize; + length = 0; + continue; + } + + map.insert(ch.clone(), idx as i32); + + length += 1; + idx += 1; + } + + if length > max_length { + max_length = length; + } + + max_length + } +} |
