So I have been trying to solve a practice problem where given a string, return the longest unique substring that occurs within that string. for example, if the string was "aab" the output of my function should be "ab" .
Below is my attempted solution, when I run it the function never ends, so I looked at it step by step and noticed that my function never progresses past the first while loop, but I am unsure why.enter image description here
Is your question about substring without repeat? I try your code, it stuck because data.get(s[j], -1) keep being 0,so your j keep being 1 and stuck in the loop. You can debug it and change your way to find repeat char. Here is a code may give you some help.
def LongestSubstring(s: str):
i = 0
if len(s) == 0:
return None
while i < len(s):
sub_size = len(s) - i
start = 0
while start < (len(s) - sub_size + 1):
sub = s[start:(start + sub_size)]
start = start + 1
for k in sub:
counts = sub.count(k)
if counts > 1:
break
if k == sub[len(sub) - 2]:
return sub
i = i + 1