Two pointers, a general way to handle this kind of problems(79,159,340).


  • 0
    K
    int lengthOfLongestSubstring(string s) {
            int m[128]={0};
            int start=0,n=s.size(),end=0,d=0,counter=0;
            while(end<n)
            {
                if(++m[s[end++]]==2)counter++;// move the right pointer
                while(counter>0)// if there is duplicate number, just move the left window
                {
                    if(--m[s[start++]]==1)counter--;
                }
                d=max(d,end-start);
            }
            return d;
        }
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.