From 07b0eab7aa8ca1165427e1e5b7058fec7dee63cb Mon Sep 17 00:00:00 2001 From: Johan Sandoval Date: Wed, 22 Jul 2026 12:17:30 -0500 Subject: [PATCH] Finish problem 0003-longest-substring-without-repeating-characters --- .../solution.py | 11 +++++++++++ .../solution_test.py | 14 ++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 problems/0003-longest-substring-without-repeating-characters/solution.py create mode 100644 problems/0003-longest-substring-without-repeating-characters/solution_test.py diff --git a/problems/0003-longest-substring-without-repeating-characters/solution.py b/problems/0003-longest-substring-without-repeating-characters/solution.py new file mode 100644 index 0000000..907d34e --- /dev/null +++ b/problems/0003-longest-substring-without-repeating-characters/solution.py @@ -0,0 +1,11 @@ +class Solution: + def lengthOfLongestSubstring(self, s: str) -> int: + hash = {} + longest = 0 + start = 0 + for idx, char in enumerate(s): + if char in hash and hash[char] >= start: + start = hash[char] + 1 + hash[char] = idx + longest = max(longest, idx - start + 1) + return longest diff --git a/problems/0003-longest-substring-without-repeating-characters/solution_test.py b/problems/0003-longest-substring-without-repeating-characters/solution_test.py new file mode 100644 index 0000000..6412db4 --- /dev/null +++ b/problems/0003-longest-substring-without-repeating-characters/solution_test.py @@ -0,0 +1,14 @@ +import pytest +from solution import Solution + + +@pytest.mark.parametrize( + "s,expected", + [ + ("abcabcbb", 3), + ("bbbbb", 1), + ("pwwkew", 3), + ], +) +def test_longest_substring(s, expected): + assert Solution().lengthOfLongestSubstring(s) == expected