首页>itarticle>159. longest substring with at most two distinct characters
159. longest substring with at most two distinct characters
admin11月 12, 20200
# Given a string, find the length of the longest substring T# that contains at most 2 distinct characters.## For example, Given s = "eceba",## T is "ece" which its length is 3.#classSolution:# @param s, a string# @return an integerdeflengthOfLongestSubstringTwoDistinct(self,s):iflen(s)==0:return0curlen=1maxlen=1flag=0foriinrange(1,len(s)):news="".join(set(s[flag:i+1]))iflen(news)<=2:curlen+=1maxlen=max(maxlen,curlen)else:curlen=1flag=ireturnmaxlenif__name__=='__main__':answer=Solution()printanswer.lengthOfLongestSubstringTwoDistinct('cecbaaa')
近期评论