0

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

  • @alex i think this is a completely different question – packetsniffer Apr 29 '21 at 07:41
  • You're asking why `max()` returns `cb` instead of e.g. `abc`. It performs a letter by letter comparison. `c` is a `99` in ASCII and `a` is a `97` so `c` > `a` so `cb` > `abc`. Only if it was the same letter being compared, the comparison would go on. It is called lexicographical ordering. – alex Apr 29 '21 at 07:47

0 Answers0