aboutsummaryrefslogtreecommitdiff
path: root/substring.py
diff options
context:
space:
mode:
Diffstat (limited to 'substring.py')
-rw-r--r--substring.py20
1 files changed, 20 insertions, 0 deletions
diff --git a/substring.py b/substring.py
new file mode 100644
index 0000000..4390dd7
--- /dev/null
+++ b/substring.py
@@ -0,0 +1,20 @@
+def longestSubstring(s: str) -> int:
+ if s == "":
+ return 0
+ start = 0
+ end = 0
+ maxLength = 0
+ uniqueCharacter = set()
+
+ while end < len(s):
+ if s[end] not in uniqueCharacter:
+ uniqueCharacter.add(s[end])
+ end += 1
+ maxLength = max(maxLength, len(uniqueCharacter))
+ else:
+ uniqueCharacter.remove(s[start])
+ start += 1
+ return maxLength
+
+#should output 3, as "abc" is the longest substring that occurs
+print(longestSubstring("abcabcbb"))