Number of Ways to Select Buildings LeetCode solution in Python

1
class Solution:
2
def numberOfWays(self, s: str) -> int:
3

4
temp = []
5
c0 = 0
6
c1 = 0
7
for char in s:
8
if char == "0":
9
c0 += 1
10
else:
11
c1 += 1
12
temp.append([c0, c1])
13

14
total0 = c0
15
total1 = c1
16

17
count = 0
18
for i in range(1, len(s) - 1):
19

20
if s[i] == "0":
21
m1 = temp[i - 1][1]
22
m2 = total1 - temp[i][1]
23
count += m1 * m2
24

25
else:
26
m1 = temp[i - 1][0]
27
m2 = total0 - temp[i][0]
28
count += m1 * m2
29
return count

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0