2222. Number of Ways to Select Buildings

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 2222

class Solution:
    def numberOfWays(self, s: str) -> int:
        n = len(s)
        cnt0 = s.count("0")
        cnt1 = n - cnt0
        c0 = c1 = 0
        res = 0
        for c in s:
            if c == "0":
                res += c1 * (cnt1 - c1)
                c0 += 1
            else:
                res += c0 * (cnt0 - c0)
                c1 += 1
        return res