!!NR!!-M-3. Longest Substring Without Repeating Characters

# 1 Sliding window  
class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        res_max = 0
        if s == '':
            return res_max
        elif len(s) == 1:
            return 1
        
        i = 0
        j = i + 1
        
        while i < len(s) and j < len(s):
            str_set = {}
            str_set[s[i]] = (s[i], i)
            j = i + 1
            cur_max = 1
            while j < len(s):
                if s[j] not in str_set:
                    str_set[s[j]] = (s[j], j)
                    cur_max += 1
                    j += 1
                else:
                    i = str_set[s[i]][1] + 1
                    break

            res_max = res_max if res_max > cur_max else cur_max
            continue
            

        return res_max

Last updated

Was this helpful?