Skip to content

Commit a7a7ca5

Browse files
authored
Merge pull request #73 from ghimiresdp/feature/longest-substring
Dynamic Programming: Longest substring length without duplicates
2 parents 91846ad + 852c51b commit a7a7ca5

4 files changed

Lines changed: 75 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ cargo test --bin huffman
139139
7. [Coin Change Problem](problem-solving/src/dp/coin_change.rs) `cargo run --bin coin_change`
140140
8. [Palindrome Partition](problem-solving/src/dp/palindrome_partition.rs) `cargo run --bin palindrome_partition`
141141
9. [Find Nth distinct number](problem-solving/src/dp/nth-distinct-number.rs) `cargo run --bin nth-distinct-number`
142+
10. [Find the length of the longest substring without duplicates](problem-solving/src/dp/longest-substring.rs) `cargo run --bin longest-substring`
142143

143144
## [5. Advanced Concepts](advanced/README.md)
144145

problem-solving/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,7 @@ path = 'src/dp/palindrome_partition.rs'
7777
[[bin]]
7878
name = 'nth-distinct-number'
7979
path = 'src/dp/nth-distinct-number.rs'
80+
81+
[[bin]]
82+
name = 'longest-substring'
83+
path = 'src/dp/longest-substring.rs'

problem-solving/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@
2424
7. [Coin Change Problem](src/dp/coin_change.rs) `cargo run --bin coin_change`
2525
8. [Palindrome Partition](src/dp/palindrome_partition.rs) `cargo run --bin palindrome_partition`
2626
9. [Find Nth distinct number](src/dp/nth-distinct-number.rs) `cargo run --bin nth-distinct-number`
27+
10. [Find the length of the longest substring without duplicates](src/dp/longest-substring.rs) `cargo run --bin longest-substring`
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//! # Longest Substring without duplicates
2+
//!
3+
//! Given a string `s`, find the length of the longest substring without
4+
//! duplicate characters.
5+
//!
6+
//! Here, we check if the duplicate exists using the sliding window.
7+
//!
8+
//! we start with the hashmap that stores the character, and it's occurrence
9+
//! index. if the duplicate is found, left index of that character is moved
10+
//! so that the starting point of the count will be from that index + 1.
11+
//!
12+
//! at the end of the loop, we check if we achieved max length by comparing prev
13+
//! max length, and updating it.
14+
15+
use std::{cmp::max, collections::HashMap};
16+
17+
fn length_of_longest_substring(string: String) -> usize {
18+
// return the length of string if the length is 0 or 1 since it has unique elements
19+
if string.len() < 2 {
20+
return string.len();
21+
}
22+
23+
let mut left = 0;
24+
let mut max_len = 0;
25+
let mut char_map: HashMap<char, usize> = HashMap::new();
26+
27+
for (right, char) in string.chars().enumerate() {
28+
if let Some(&index) = char_map.get(&char) {
29+
if index >= left {
30+
left = index + 1;
31+
}
32+
}
33+
char_map.insert(char, right);
34+
max_len = max(max_len, right - left + 1);
35+
// TODO: uncomment to see assigned values
36+
// println!("character: {char}, left: {left}, right: {right}, max_len: {max_len}");
37+
}
38+
return max_len;
39+
}
40+
fn main() {
41+
let length = length_of_longest_substring("abcabcbb".to_owned());
42+
println!("length_of_the_longest_substring: {length}");
43+
}
44+
45+
#[cfg(test)]
46+
mod tests {
47+
use crate::length_of_longest_substring;
48+
49+
#[test]
50+
fn empty_string() {
51+
assert_eq!(length_of_longest_substring("".to_owned()), 0);
52+
}
53+
#[test]
54+
fn single_item() {
55+
assert_eq!(length_of_longest_substring("a".to_owned()), 1);
56+
}
57+
#[test]
58+
fn two_items() {
59+
assert_eq!(length_of_longest_substring("au".to_owned()), 2);
60+
}
61+
#[test]
62+
fn test_for_correctness() {
63+
assert_eq!(length_of_longest_substring("abcabcbb".to_owned()), 3);
64+
assert_eq!(length_of_longest_substring("bbbbb".to_owned()), 1);
65+
assert_eq!(length_of_longest_substring("bbbabb".to_owned()), 2);
66+
assert_eq!(length_of_longest_substring("pwwkew".to_owned()), 3);
67+
assert_eq!(length_of_longest_substring("abcbcad".to_owned()), 4);
68+
}
69+
}

0 commit comments

Comments
 (0)