diff --git a/README.md b/README.md index 3092ce1..7fc4dee 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # leetcode +I am starting out by doing the Blind 75, then Grind 75 \ No newline at end of file diff --git a/medium/3 Longest Substring Without Repeating Characters/solution.py b/medium/3 Longest Substring Without Repeating Characters/solution.py new file mode 100644 index 0000000..3741158 --- /dev/null +++ b/medium/3 Longest Substring Without Repeating Characters/solution.py @@ -0,0 +1,33 @@ +class Solution: + def lengthOfLongestSubstring(self, s: str) -> int: + """ + >>> Solution().lengthOfLongestSubstring("abcabcbb") + 3 + >>> Solution().lengthOfLongestSubstring("bbbbb") + 1 + >>> Solution().lengthOfLongestSubstring("pwwkew") + 3 + """ + if s == "": + return 0 + + beginning = 0 + end = 1 + longest = 0 + while end < len(s): + p = s[beginning:end] + if s[end] in s[beginning:end]: + if end - beginning > longest: + longest = end - beginning + beginning += 1 + else: + end += 1 + + if end - beginning > longest: + longest = end - beginning + return longest + + +if __name__ == "__main__": + import doctest + doctest.testmod() \ No newline at end of file