Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ cargo test --bin huffman
7. [Coin Change Problem](problem-solving/src/dp/coin_change.rs) `cargo run --bin coin_change`
8. [Palindrome Partition](problem-solving/src/dp/palindrome_partition.rs) `cargo run --bin palindrome_partition`
9. [Find Nth distinct number](problem-solving/src/dp/nth-distinct-number.rs) `cargo run --bin nth-distinct-number`
10. [Find the length of the longest substring without duplicates](problem-solving/src/dp/longest-substring.rs) `cargo run --bin longest-substring`

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

Expand Down
4 changes: 4 additions & 0 deletions problem-solving/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,7 @@ path = 'src/dp/palindrome_partition.rs'
[[bin]]
name = 'nth-distinct-number'
path = 'src/dp/nth-distinct-number.rs'

[[bin]]
name = 'longest-substring'
path = 'src/dp/longest-substring.rs'
1 change: 1 addition & 0 deletions problem-solving/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@
7. [Coin Change Problem](src/dp/coin_change.rs) `cargo run --bin coin_change`
8. [Palindrome Partition](src/dp/palindrome_partition.rs) `cargo run --bin palindrome_partition`
9. [Find Nth distinct number](src/dp/nth-distinct-number.rs) `cargo run --bin nth-distinct-number`
10. [Find the length of the longest substring without duplicates](src/dp/longest-substring.rs) `cargo run --bin longest-substring`
69 changes: 69 additions & 0 deletions problem-solving/src/dp/longest-substring.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//! # Longest Substring without duplicates
//!
//! Given a string `s`, find the length of the longest substring without
//! duplicate characters.
//!
//! Here, we check if the duplicate exists using the sliding window.
//!
//! we start with the hashmap that stores the character, and it's occurrence
//! index. if the duplicate is found, left index of that character is moved
//! so that the starting point of the count will be from that index + 1.
//!
//! at the end of the loop, we check if we achieved max length by comparing prev
//! max length, and updating it.

use std::{cmp::max, collections::HashMap};

fn length_of_longest_substring(string: String) -> usize {
// return the length of string if the length is 0 or 1 since it has unique elements
if string.len() < 2 {
return string.len();
}

let mut left = 0;
let mut max_len = 0;
let mut char_map: HashMap<char, usize> = HashMap::new();

for (right, char) in string.chars().enumerate() {
if let Some(&index) = char_map.get(&char) {
if index >= left {
left = index + 1;
}
}
char_map.insert(char, right);
max_len = max(max_len, right - left + 1);
// TODO: uncomment to see assigned values
// println!("character: {char}, left: {left}, right: {right}, max_len: {max_len}");
}
return max_len;
}
fn main() {
let length = length_of_longest_substring("abcabcbb".to_owned());
println!("length_of_the_longest_substring: {length}");
}

#[cfg(test)]
mod tests {
use crate::length_of_longest_substring;

#[test]
fn empty_string() {
assert_eq!(length_of_longest_substring("".to_owned()), 0);
}
#[test]
fn single_item() {
assert_eq!(length_of_longest_substring("a".to_owned()), 1);
}
#[test]
fn two_items() {
assert_eq!(length_of_longest_substring("au".to_owned()), 2);
}
#[test]
fn test_for_correctness() {
assert_eq!(length_of_longest_substring("abcabcbb".to_owned()), 3);
assert_eq!(length_of_longest_substring("bbbbb".to_owned()), 1);
assert_eq!(length_of_longest_substring("bbbabb".to_owned()), 2);
assert_eq!(length_of_longest_substring("pwwkew".to_owned()), 3);
assert_eq!(length_of_longest_substring("abcbcad".to_owned()), 4);
}
}