python递归实现查找字符串中不重复的最长字符串

def lengthOfLongestSubstring(s):
    print(s)
    if s is not None:
        temp = s[0]
        num = 1
        for i, ch in enumerate(s[1:]):
            if ch in temp:
                result.setdefault(temp, num)
                return lengthOfLongestSubstring(s[1:])
            else:
                num += 1
                temp = temp + ch

if __name__ == '__main__':
    result = {
    
    }
    s = 'bcdebafga'
    lengthOfLongestSubstring(s)
    print(result)
    print(max(result, key=result.get), max(result.values()))

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_46304253/article/details/111867628