怕NPY的牛牛 滑动窗口解法
牛牛非常怕他的女朋友,怕到了走火入魔的程度,以至于每当他看到一个字符串同时含有n,p,y三个字母他都害怕的不行。现在有一个长度为m的只包含小写字母‘a’-‘z’的字符串x,牛牛想知道能令他不害怕的最长子串的长度是多少。(对于字符串”abc”来说,”c”,”ab”都是原串的子串,但”ac”不是原串子串) class Solution: def Maximumlength(self , x ): result=-1 length=len(x) current=[] flag={"n":0,"p":0,'y':0} for i in range(length): current.append(x[i]) if x[i]=="n": flag["n"]=flag.get("n",0)+1 if x[i]=="p": flag["p"]=flag.get("p",0)+1 if x[i] == "y": flag["y"]=flag.get("y",0)+1 if flag["n"]>0 and flag["p"]>0 and flag["y"]>0: a=current.pop(0) if a == "n": flag["n"] = flag.get("n", 0) - 1 if a == "p": flag["p"] = flag.get("p", 0) - 1 if a == "y": flag["y"] = flag.get("y", 0) - 1 while flag["n"]!=0 and flag["p"]!=0 and flag["y"]!=0: a=current.pop(0) if a == "n": flag["n"] = flag.get("n", 0) - 1 if a == "p": flag["p"] = flag.get("p", 0) - 1 if a == "y": flag["y"] = flag.get("y", 0) - 1 result=max(result,len(current)) return result
#题解#