Consider the following script
def fun(s):
lst = []
for i in range (len(s)):
for j in range(i , len(s)):
if not s[i:j] :
pass
elif len(s[i:j]) == len(set(s[i:j])):
lst.append(s[i:j])
print (lst )
print (max(lst))
s = 'abcabcbb'
fun(s)
The goal is to find the longest substring without repeating characters
when you run the script you get the output cb
which is not the right answer
lst
has the following elements
['a', 'ab', 'abc', 'b', 'bc', 'bca', 'c', 'ca', 'cab', 'a', 'ab', 'abc', 'b', 'bc', 'c', 'cb', 'b']
why does max()
return cb
instead of bca
, abc
, cab
if max is referring to the index then the result should be b
if max is referring to the length of the element then the result should be one of these bca
, abc
, cab