美文网首页Leetcode
Leetcode 696. Count Binary Subst

Leetcode 696. Count Binary Subst

作者: SnailTyan | 来源:发表于2021-05-06 16:39 被阅读0次

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Count Binary Substrings

2. Solution

  • Version 1
class Solution:
    def countBinarySubstrings(self, s: str) -> int:
        length = len(s)
        count = 0
        pre = 0
        curr = 1
        for i in range(1, length):
            if s[i] == s[i - 1]:
                curr += 1
            else:
                count += min(pre, curr)
                pre = curr
                curr = 1
        count += min(pre, curr)
        return count

Reference

  1. https://leetcode.com/problems/count-binary-substrings/

相关文章

网友评论

    本文标题:Leetcode 696. Count Binary Subst

    本文链接:https://www.haomeiwen.com/subject/ijeddltx.html