Simple python, >94%,


  • 0
    C
    def lengthOfLongestSubstringTwoDistinct(self, s):
        if len(s)<=2:
            return len(s)
        maxLen=0
        windowSet=s[:1] if s[1]==s[0] else s[:2]
        lastPureStart=0 if s[1]==s[0] else 1
        curLen=2
    
        for i in xrange(2,len(s)):
            if s[i] in windowSet:
                curLen+=1
                if s[i]!=s[i-1]:
                    lastPureStart=i
            elif len(windowSet)==1:
                windowSet+=s[i]
                curLen+=1
                lastPureStart=i
            else:
                if maxLen<curLen:
                    maxLen=curLen
    
                curLen=i-lastPureStart+1
                windowSet=s[i-1:i+1]
                lastPureStart=i
        return max(maxLen, curLen)

Log in to reply
 

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