Skip to content

Latest commit

 

History

History
30 lines (21 loc) · 612 Bytes

3.md

File metadata and controls

30 lines (21 loc) · 612 Bytes

[3] Longest Substring Without Repeating Characters

Description

link


Solution : TP

  • See Code

Code

Complexity T : O(N) M : O(1)

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        k, res, c_dict = -1, 0, {}
        for i, c in enumerate(s):
            if c in c_dict and c_dict[c] > k:  # 字符c在字典中 且 上次出现的下标大于当前长度的起始下标
                k = c_dict[c]
            else:
                res = max(res, i-k)
            c_dict[c] = i
        return res