Longest Substring Without Repeating Characters (Leetcode 3)

Problem Link: https://leetcode.com/problems/longest-substring-without-repeating-characters/

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        n=len(s)
        ans=0
        j=0
        d={}
        for i in range(n):
            if(s[i] not in d):
                d[s[i]]=1
            else:
                d[s[i]]+=1
                while(s[i]!=s[j]):
                    del d[s[j]]
                    j+=1
                d[s[j]]-=1
                j+=1
            
            ans=max(ans,i-j+1)
        return ans          

Last updated