Leetcode Problem 2222. Number of Ways to Select Buildings
2222. Number of Ways to Select Buildings
Leetcode Solutions
Prefix Sum Approach
Initialize two variables to count the total number of '0's and '1's in the string.
Initialize two variables to keep track of the number of '0's and '1's encountered so far as we iterate through the string.
Iterate through the string from left to right.
For each character, if it is '0', calculate the product of the count of '1's before this '0' and the count of '1's after this '0'. Add this product to the answer.
If the character is '1', calculate the product of the count of '0's before this '1' and the count of '0's after this '1'. Add this product to the answer.
Update the count of '0's or '1's encountered so far.