Skip to content

Commit e208dae

Browse files
committed
Added problems: 3 - Longest Substring Without Repeating Characters
1 parent 30b7521 commit e208dae

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Note that this license applies only to my solution code and not to the LeetCode
3333
| Problem | Solution | Approach |
3434
|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------|:---------------------------------------------------------------------------------------:|:-----------------------------------|
3535
| 🟢 1. [Two Sum](https://leetcode.com/problems/two-sum/) | [🦀](src/problems/p0001_two_sum.rs) | Hash Map |
36+
| 🟠 3. [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [🦀](src/problems/p0003_longest_substring_without_repeating_characters.rs) | HashMap, Two Pointers |
3637
| 🟢 13. [Roman to Integer](https://leetcode.com/problems/roman-to-integer/) | [🦀](src/problems/p0013_roman_to_integer.rs) | |
3738
| 🟢 20. [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [🦀](src/problems/p0020_valid_parentheses.rs) | Stack |
3839
| 🟢 21. [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | [🦀](src/problems/p0021_merge_two_sorted_lists.rs) | |

src/problems/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod p0001_two_sum;
2+
pub mod p0003_longest_substring_without_repeating_characters;
23
pub mod p0013_roman_to_integer;
34
pub mod p0020_valid_parentheses;
45
pub mod p0021_merge_two_sorted_lists;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//! # LeetCode Problem: 3 - Longest Substring Without Repeating Characters
2+
//!
3+
//! Difficulty: Medium
4+
//!
5+
//! Link: https://leetcode.com/problems/longest-substring-without-repeating-characters/
6+
//!
7+
//! ## Complexity Analysis
8+
//! - Time Complexity: O(n) - We traverse the string containing n characters exactly once.
9+
//! - Space Complexity: O(n) - The extra space required depends on the number of items
10+
//! stored in the hash table.
11+
12+
pub struct Solution;
13+
14+
impl Solution {
15+
pub fn length_of_longest_substring(s: String) -> i32 {
16+
if s.len() <= 1 {
17+
return s.len() as i32;
18+
}
19+
20+
let mut result = 0;
21+
let mut left = 0;
22+
let mut letters = std::collections::HashMap::new();
23+
24+
for (right, c) in s.chars().enumerate() {
25+
if let Some(&prev_idx) = letters.get(&c) {
26+
left = std::cmp::max(prev_idx + 1, left);
27+
}
28+
letters.insert(c, right);
29+
result = std::cmp::max(result, (right - left + 1) as i32);
30+
}
31+
32+
result
33+
}
34+
}
35+
36+
#[cfg(test)]
37+
mod tests {
38+
use super::Solution;
39+
40+
#[test]
41+
fn test_length_of_longest_substring() {
42+
let test_cases = [("abcabcbb", 3), ("bbbbb", 1), ("pwwkew", 3)];
43+
for (idx, (input, expected)) in test_cases.iter().enumerate() {
44+
let result = Solution::length_of_longest_substring(input.to_string());
45+
assert_eq!(
46+
result, *expected,
47+
"Test case #{idx}: with input {input:?}, expected {expected:?}, got {result:?}"
48+
);
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)