<p>You are given a <strong>0-indexed</strong> binary string <code>s</code> which represents the types of buildings along a street where:</p>

<ul>
	<li><code>s[i] = &#39;0&#39;</code> denotes that the <code>i<sup>th</sup></code> building is an office and</li>
	<li><code>s[i] = &#39;1&#39;</code> denotes that the <code>i<sup>th</sup></code> building is a restaurant.</li>
</ul>

<p>As a city official, you would like to <strong>select</strong> 3 buildings for random inspection. However, to ensure variety, <strong>no two consecutive</strong> buildings out of the <strong>selected</strong> buildings can be of the same type.</p>

<ul>
	<li>For example, given <code>s = &quot;0<u><strong>0</strong></u>1<u><strong>1</strong></u>0<u><strong>1</strong></u>&quot;</code>, we cannot select the <code>1<sup>st</sup></code>, <code>3<sup>rd</sup></code>, and <code>5<sup>th</sup></code> buildings as that would form <code>&quot;0<strong><u>11</u></strong>&quot;</code> which is <strong>not</strong> allowed due to having two consecutive buildings of the same type.</li>
</ul>

<p>Return <em>the <b>number of valid ways</b> to select 3 buildings.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;001101&quot;
<strong>Output:</strong> 6
<strong>Explanation:</strong> 
The following sets of indices selected are valid:
- [0,2,4] from &quot;<u><strong>0</strong></u>0<strong><u>1</u></strong>1<strong><u>0</u></strong>1&quot; forms &quot;010&quot;
- [0,3,4] from &quot;<u><strong>0</strong></u>01<u><strong>10</strong></u>1&quot; forms &quot;010&quot;
- [1,2,4] from &quot;0<u><strong>01</strong></u>1<u><strong>0</strong></u>1&quot; forms &quot;010&quot;
- [1,3,4] from &quot;0<u><strong>0</strong></u>1<u><strong>10</strong></u>1&quot; forms &quot;010&quot;
- [2,4,5] from &quot;00<u><strong>1</strong></u>1<u><strong>01</strong></u>&quot; forms &quot;101&quot;
- [3,4,5] from &quot;001<u><strong>101</strong></u>&quot; forms &quot;101&quot;
No other selection is valid. Thus, there are 6 total ways.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;11100&quot;
<strong>Output:</strong> 0
<strong>Explanation:</strong> It can be shown that there are no valid selections.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>3 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
	<li><code>s[i]</code> is either <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li>
</ul>

class Solution:
    def numberOfWays(self, s: str) -> int:
        # ans = 0
        # one = zero = zero_one = one_zero = 0
        # for c in s:
        #     if c == '0':
        #         zero += 1
        #         one_zero += one
        #         ans += zero_one
        #     else:
        #         one += 1    
        #         zero_one += zero 
        #         ans += one_zero
        # return ans
        n = len(s)
        zero_left = [0 for _ in range(n)]
        zero_right = [0 for _ in range(n)]
        one_left = [0 for _ in range(n)]
        one_right = [0 for _ in range(n)]
        zero_l = one_l = 0
        for i in range(n):
            if s[i] == "0":
                zero_l += 1
            else:
                one_l += 1
            one_left[i] = one_l
            zero_left[i] = zero_l
        zero_r = one_r = 0
        for i in range(n - 1, -1, -1):
            if s[i] == "0":
                zero_r += 1
            else:
                one_r += 1
            
            zero_right[i] = zero_r
            one_right[i] = one_r
        ans = 0
        for j in range(1, n - 1):
            if s[j] == "1":
                ans += zero_left[j - 1] * zero_right[j + 1]
            else:
                ans += one_left[j - 1] * one_right[j + 1]
        return ans
        # Follow up - solution
        # dp = [[0, 0] for _ in range(3)]
        # for c in s:
        #     idx = int(c)
        #     dp[0][idx] += 1
        #     for i in range(1, 3):
        #         dp[i][idx] += dp[i - 1][1 - idx]
        # return sum(dp[-1])
            
1019