3. Longest Substring Without Repeating Characters

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


def lengthOfLongestSubstring(self, s: str) -> int:      

        max_len = 0
       
        left, right = 0, 0
        last_seen = {}
       
        while right < len(s):  
            ch = s[right]
                       
            if ch in last_seen and last_seen[ch] >= left:
                left = last_seen[ch] + 1
           
            last_seen[ch] = right
                                 
            max_len = max(max_len, right-left + 1)
            right += 1  
           
        return max_len

Comments

Popular posts from this blog

849. Maximize Distance to Closest Person

347. Top K Frequent Elements

139. Word Break