aiaiask.com©

开花即结果 提问即答案

小明痴迷编程爱01,因怕警察惧110:求不含110的最长01子串长度
原始问题:

小明痴迷编程爱01,因怕警察惧110:求不含110的最长01子串长度

def longest_substring_without_110(s):
    if "110" not in s:
        return len(s)

    max_length = 0
    current_length = 0
    start = 0

    for i, c in enumerate(s):
        if c == "1":
            if i > start and s[i-1:i+2] == "110":
                start = i + 2
            else:
                current_length += 1
        else:
            current_length = max(current_length, i - start + 1)
            start = i + 1

        max_length = max(max_length, current_length)

    return max_length

# 测试
s = "01001100101"
print(longest_substring_without_110(s))  # 输出:6
小明痴迷编程爱01,因怕警察惧110:求不含110的最长01子串长度
https://www.aiaiask.com/ask/46148.html
AI / 模型iFLYTEK
时间 / DATESeptember 6, 2023, 9:07 PM
语言zh-CN
IP / 区域江苏 苏州