zltzlt 发表于 2020-2-17 21:20:12

Python:每日一题 334

今天的题目:

给定一个只包含 a 和 b 的字符串,找出不包含 aaa 或 bbb 的最长子字符串的长度。

示例 1:

输入:"baaabbabbb"
输出:7
解释:"aabbabb" 是最长符合条件的子字符串。
示例 2:

输入:"babba"
输出:5
解释:整个字符串符合条件。
示例 3:

输入:"abaaaa"
输出:4
解释:"abaa" 是最长符合条件的子字符串。

{:10_298:}欢迎大家一起答题!{:10_298:}

fan1993423 发表于 2020-2-17 21:25:08

终于等到楼主出新题了,最近效率不高啊

zltzlt 发表于 2020-2-17 21:25:54

fan1993423 发表于 2020-2-17 21:25
终于等到楼主出新题了,最近效率不高啊

没有啊,一天一题(除了悬赏题)

fan1993423 发表于 2020-2-17 21:53:16

from itertools import groupby
from bisect import bisect_left
def fun314(s):
    t,count,result=[],0,0
    for k,v in groupby(s):
      t.append(len(list(v)))
    if bisect_left(t,3)==len(t):return sum(t)
    else:
      for i in t:
            if i<3:
                count+=i
            else:
                count+=2
                result=max(count,result)
                count=2
    return result
先写一个,可能效率不高

zltzlt 发表于 2020-2-17 21:56:32

fan1993423 发表于 2020-2-17 21:53
先写一个,可能效率不高

560 ms

塔利班 发表于 2020-2-17 21:59:06

def f334(x):
    m,ca,cb,s=0,0,0,0
    for e in x:
      s+=1
      if e=='a':
            cb=0
            ca+=1
            if ca==3:
                m=max(s-1,m)
                ca=0
                s=2
      else:
            ca=0
            cb+=1
            if cb==3:
                m=max(s-1,m)
                cb=0
                s=2

    return max(s,m)

William4869 发表于 2020-2-17 22:26:25

本帖最后由 William4869 于 2020-2-17 22:47 编辑

def f334(s):
    set1=set()
    temp=0
    set1.add(0)
    for i in range(len(s)-2):
      if s==s and s==s:
            set1.add(i+2-temp)
            temp=i+1
      elif i==len(s)-3:
            set1.add(i+3-temp)
    return max(set1)

print(f334("aabbaaadafaasfa"))

试试

fan1993423 发表于 2020-2-17 22:33:58

在写个方法
def fun314(s):
    flag,count,result=True,0,0
    for i in range(len(s)-2):
      if len(set(s))==2 and flag:
            count+=3
            flag=False
      elif len(set(s))==2 and not flag:
            count+=1
      elif len(set(s))==1:
            result=max(count,result)
            count,flag=0,True
    return max(count,result)

wanting-for 发表于 2020-2-17 22:38:44

def solve(s:str):
    len_s, i, a_num, b_num, res, max_s = len(s), 0, 0, 0, 0, float('-inf')
    while i < len_s:
      res += 1
      if s == 'a':
            a_num += 1
            b_num = 0
      else:
            a_num = 0
            b_num += 1
      if a_num == 3 or b_num == 3:
            if res - 1 > max_s:
                max_s = res - 1
            res,a_num, b_num = 0, 0, 0
            i -= 1
            continue
      i += 1
    return max_s if max_s > res else res
敲了一下午 c 有点累。。{:5_108:}
不过感觉这道题,似曾相识。。

546623863 发表于 2020-2-17 22:39:45

def fun334(s : str):
    if(s == ""):
      return 0
    Max = 0
    index = 2
    begin = 0
    length = len(s)
    while index < length:
      if(s == s and s == s):
            Max = max((index-begin),Max)
            begin = index-1
      index += 1
    if(begin == 0):
      return length
    return Max

随手写的玩的,不知道效率咋样

ouyunfu 发表于 2020-2-17 22:40:15

本帖最后由 ouyunfu 于 2020-2-17 22:42 编辑

def f334(s:str)->int:
    L,count,n_a,n_b=[],0,0,0
    for i in s:
      if i=='a':
            n_b=0
            if n_a<2:
                n_a+=1
                count+=1
            else:
                L.append(count)
                n_a=2
                count=2   
      else:
            n_a=0
            if n_b<2:
                n_b+=1
                count+=1
            else:
                L.append(count)
                n_b=2
                count=2
    L.append(count)
    return max(L)

fan1993423 发表于 2020-2-17 22:40:39

wanting-for 发表于 2020-2-17 22:38
敲了一下午 c 有点累。。
不过感觉这道题,似曾相识。。

你还在敲C代码啊,可以的

wanting-for 发表于 2020-2-17 22:46:03

fan1993423 发表于 2020-2-17 22:40
你还在敲C代码啊,可以的

毕竟不能永远陶醉在Python的温室啊,
还是得敲一下c的
但是 c 好难敲啊{:5_99:}

William4869 发表于 2020-2-17 22:48:55

def f334(s):
    temp,i,ret=0,0,0
    while i<len(s)-2:
      if s==s and s==s:
            ret=max(i+2-temp,ret)
            temp,i=i+1,i+3
      elif i==len(s)-3:
            ret=max(i+3-temp,ret)
            i+=1
      else: i+=1
    return ret


也不知道会不会快一点

fan1993423 发表于 2020-2-17 22:50:55

wanting-for 发表于 2020-2-17 22:46
毕竟不能永远陶醉在Python的温室啊,
还是得敲一下c的
但是 c 好难敲啊

为你点赞

ruokang 发表于 2020-2-17 23:09:00

str1 = 'baaabbabbb'
length =
for i in range(len(str1)):
        for j in range(len(str1) - i):
                substr = str1
                if ('aaa' not in substr) and ('bbb' not in substr):
                        length.append(len(substr))
print(max(length))

kinkon 发表于 2020-2-18 00:06:02

def f334(s):
    ab = ''
    c = t = k = i = 0
    while i < len(s):
      if s == ab:
            k += 1
            t += 1
            if k == 3:
                c = max(t - 1, c)
                t, ab = 0, ''
                i -= 2
            i += 1
      else:
            ab, k = s, 1
            t += 1
            i += 1
    return c if c else t

阴阳神万物主 发表于 2020-2-18 00:42:09

叹息,用了双for
def solve(s:str)->int:
    res = 0
    le = len(s)
    for l in range(le):
      for r in range(l+res,le+1):
            now = s
            if ('aaa'in now)or('bbb'in now):
                break
            res=max(res,r-l)
      if le-l<res:break
    return res
if __name__ == '__main__':
    print('示例1 输出:',solve("baaabbabbb"))
    print('示例2 输出:',solve("babba"))
    print('示例3 输出:',solve("abaaaa"))

kinkon 发表于 2020-2-18 01:05:16

再来一种方法吧
def p334(s):
    i = t = c = 0
    r = ''
    while i < len(s):
      r = s
      if 'aaa' == r or 'bbb' == r:
            c = max(t + 2, c)
            i += 1
            t = 0
      else:
            i += 1
            t += 1
    return c if c else t

graceasyi 发表于 2020-2-18 09:40:05

def fun334(str_in):
        s1pa = str_in.split("aaa")
        s1pb =
        s1p = sum(s1pb, [])
        t1 = max(s1p, key=lambda x: len(x))
        c = str_in.index(t1)
        l = len(t1)
        result = t1
        if c:
                if str_in != str_in:
                        result = str_in + result
                else:
                        if str_in != str_in:
                                result = str_in + result
        if c + l < len(str_in):
                if str_in != str_in:
                        result = result + str_in
                else:
                        if str_in != str_in:
                                result = result + str_in
        return len(result)
页: [1] 2 3
查看完整版本: Python:每日一题 334