From 3a30e52296ab7a509275cde367629d9a10cf3416 Mon Sep 17 00:00:00 2001 From: Deven <63876261+devenperez@users.noreply.github.com> Date: Wed, 18 May 2022 16:56:54 -0400 Subject: [PATCH] Time: 4083 ms (7.65%), Space: 15.5 MB (5.68%) - LeetHub --- ...-substring-without-repeating-characters.py | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 3-longest-substring-without-repeating-characters/3-longest-substring-without-repeating-characters.py diff --git a/3-longest-substring-without-repeating-characters/3-longest-substring-without-repeating-characters.py b/3-longest-substring-without-repeating-characters/3-longest-substring-without-repeating-characters.py new file mode 100644 index 0000000..df1d51b --- /dev/null +++ b/3-longest-substring-without-repeating-characters/3-longest-substring-without-repeating-characters.py @@ -0,0 +1,26 @@ +class Solution(object): + def lengthOfLongestSubstring(self, s): + """ + :type s: str + :rtype: int + """ + letters = [x for x in s] + longestLength = 1 + + if len(letters) == 0: return 0 + + for i in range(len(letters)): + dictionary = {} + for j in range(i, len(letters)): + if dictionary.get(letters[j]) == None: + dictionary[letters[j]] = 0 + else: + if j-i > longestLength: + longestLength = j-i + break + + if j == len(letters) - 1: + if j-i+1 > longestLength: + longestLength = j-i+1 + return longestLength + return longestLength \ No newline at end of file