假设我们有一个字符串s,我们必须找到具有相同字符的最长子字符串的长度。
因此,如果输入像“ abbbaccabbbba”,则输出将为4,因为有四个连续的b。
为了解决这个问题,我们将遵循以下步骤-
如果s的大小为0,则
返回0
s:= s连接空白
ct:= 1,tem:= 1
对于0到s -2大小的i
ct:= tem和ct的最大值
tem:= 1
tem:= tem + 1
如果s [i]与s [i + 1]相同,则
除此以外,
返回ct
让我们看下面的实现以更好地理解-
class Solution:
   def solve(self, s):
      if len(s)==0:
         return 0
      s+=' '
      ct=1
      tem=1
      for i in range(len(s)-1):
         if s[i]==s[i+1]:
            tem+=1
         else:
            ct=max(tem,ct)
            tem=1
      return ct
ob = Solution()print(ob.solve("abbbaccabbbba"))"abbbaccabbbba"
输出结果
4