Finish problem 0003-longest-substring-without-repeating-characters
This commit is contained in:
@@ -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
|
||||||
@@ -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
|
||||||
Reference in New Issue
Block a user