summaryrefslogtreecommitdiff
path: root/03_longest-substring-without-repeating-chars
diff options
context:
space:
mode:
Diffstat (limited to '03_longest-substring-without-repeating-chars')
-rw-r--r--03_longest-substring-without-repeating-chars/.gitignore1
-rw-r--r--03_longest-substring-without-repeating-chars/Cargo.lock7
-rw-r--r--03_longest-substring-without-repeating-chars/Cargo.toml6
-rw-r--r--03_longest-substring-without-repeating-chars/src/main.rs46
4 files changed, 60 insertions, 0 deletions
diff --git a/03_longest-substring-without-repeating-chars/.gitignore b/03_longest-substring-without-repeating-chars/.gitignore
new file mode 100644
index 0000000..ea8c4bf
--- /dev/null
+++ b/03_longest-substring-without-repeating-chars/.gitignore
@@ -0,0 +1 @@
+/target
diff --git a/03_longest-substring-without-repeating-chars/Cargo.lock b/03_longest-substring-without-repeating-chars/Cargo.lock
new file mode 100644
index 0000000..b4f61c9
--- /dev/null
+++ b/03_longest-substring-without-repeating-chars/Cargo.lock
@@ -0,0 +1,7 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 4
+
+[[package]]
+name = "longest_substring_wo_repeating_chars"
+version = "0.1.0"
diff --git a/03_longest-substring-without-repeating-chars/Cargo.toml b/03_longest-substring-without-repeating-chars/Cargo.toml
new file mode 100644
index 0000000..dadbf03
--- /dev/null
+++ b/03_longest-substring-without-repeating-chars/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "longest_substring_wo_repeating_chars"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies]
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
+ }
+}