3. Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters.

Examples:

Given "abcabcbb", the answer is "abc", which the length is 3.

Given "bbbbb", the answer is "b", with the length of 1.

Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

def long_sub(str):
    strlist = list(str)
    substrlist = list(str)
    longstr = strlist[0]
    for i in strlist:
        longlist = []
        longlist.append(i)
        substrlist.remove(i)
        for j in substrlist:
            if j in longlist:
                break
            else:
                longlist.append(j)
                if len(longlist) > len(longstr):
                    longstr = "".join(longlist)
    return longstr
    
if __name__ == "__main__":
    print(long_sub("abcabcabcqwer"))

你可能感兴趣的:(3. Longest Substring Without Repeating Characters)