<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:

        """
        option 1: 101
        option 2: 010

        10
        01
        counter the number of valid "01", and number of valid "10"

        01010 ->  5 elements, sum is 2?
        00011?
        00110?
        """

        N = len(s)

        n0 = [0] * N
        n1 = [0] * N

        n0[0] = 1 if s[0] is '0' else 0
        n1[0] = 1 if s[0] is '1' else 0

        n01 = [0] * N
        n10 = [0] * N

        for i in range(1, N):

            n0[i] = n0[i-1] +1 if s[i] is '0' else n0[i-1]
            n1[i] = n1[i-1] +1 if s[i] is '1' else n1[i-1]

        for i in range(1, N):

            # range 0 to i-1, there are k's 0, we can form k's new "01" 
            # n01 marks the number of distinct 01 pair up to index ...

            if s[i] is "1":
                
                n01[i] = n0[i-1] + n01[i-1]

                n10[i] = n10[i-1]

            elif s[i] is "0":
                
                n10[i] = n1[i-1] + n10[i-1]

                n01[i] = n01[i-1]


        n010 = [0] * N
        n101 = [0] * N

        for i in range(2, N):

            # range 0 to i-1, there are k's 0, we can form k's new "01" 
            # n01 marks the number of distinct 01 pair up to index ...

            if s[i] is "0":
                
                n010[i] = n010[i-1] + n01[i-1]

                n101[i] = n101[i-1]

            elif s[i] is "1":
                
                n101[i] = n101[i-1] + n10[i-1]

                n010[i] = n010[i-1]

        return n101[-1] + n010[-1]






        # n01 = [0]*(N)
        # n10 = [0]*(N)

        # # at index 0, only 1 element so ... 0


        # for i in range(1, N-1):
        #     for j in range(0, N-2):
        #         if s[j] == "0" and s[i] == "1":
        #             n01[i] += 1

        #         elif s[j] == "1" and s[i] == "0":
        #             n10[i] += 1

        # dp = [0] * N
        # dp[0] = 0
        # dp[1] = 0

        # for k in range(2, N):
        #     if s[k] is "1":
        #         dp[k]+=n10[k-1]

        #     elif s[k] is "0":
        #         dp[k]+=n01[k-1]

        # return sum(dp)



        
1225