1. 程式人生 > 其它 >python遞迴實現查詢字串中不重複的最長字串

python遞迴實現查詢字串中不重複的最長字串

技術標籤:演算法資料結構pythonpython演算法字串leetcode

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()))

在這裡插入圖片描述