From 6b5c05dd572b5292b36d0cd39624bb5d182f9c27 Mon Sep 17 00:00:00 2001 From: Sean Date: Wed, 22 Nov 2023 12:25:53 -0500 Subject: [PATCH] lengthOfLongestSubstring accepted submission 1 --- README.md | 1 + .../solution.py | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 medium/3 Longest Substring Without Repeating Characters/solution.py 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