share my python dynamic solution, beats 99% so far


  • 0
    W
    class Solution(object):
        def lengthOfLongestSubstringTwoDistinct(self, s):
            prevCha = ["",""]
            cum = [0,0]
            res = 0
            for c in s:
                if c != prevCha[0]:
                    if c == prevCha[1]:
                        cum[1] += cum[0]
                    else:
                        res = max(res, cum[0] + cum[1])
                        cum[1] = cum[0]
                    cum[0] = 1
                    prevCha = [c,prevCha[0]]
                else:
                    cum[0]+=1
            return max(res,cum[0]+cum[1])
    

Log in to reply
 

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