java.lang.Object
g2201_2300.s2222_number_of_ways_to_select_buildings.Solution

public class Solution extends Object
2222 - Number of Ways to Select Buildings.<p>Medium</p> <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] = '0'</code> denotes that the <code>i<sup>th</sup></code> building is an office and</li> <li><code>s[i] = '1'</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**0**1**1**0**1**&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**11**&quot;</code> which is <strong>not</strong> allowed due to having two consecutive buildings of the same type.</li> </ul> <p>Return <em>the <strong>number of valid ways</strong> to select 3 buildings.</em></p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;001101&rdquo;</p> <p><strong>Output:</strong> 6</p> <p><strong>Explanation:</strong> The following sets of indices selected are valid:</p> <ul> <li> <p>[0,2,4] from &ldquo;<strong>0</strong>0<strong>1</strong>1<strong>0</strong>1&rdquo; forms &ldquo;010&rdquo;</p> </li> <li> <p>[0,3,4] from &ldquo;<strong>0</strong>01<strong>10</strong>1&rdquo; forms &ldquo;010&rdquo;</p> </li> <li> <p>[1,2,4] from &ldquo;0<strong>01</strong>1<strong>0</strong>1&rdquo; forms &ldquo;010&rdquo;</p> </li> <li> <p>[1,3,4] from &ldquo;0<strong>0</strong>1<strong>10</strong>1&rdquo; forms &ldquo;010&rdquo;</p> </li> <li> <p>[2,4,5] from &ldquo;00<strong>1</strong>1<strong>01</strong>&rdquo; forms &ldquo;101&rdquo;</p> </li> <li> <p>[3,4,5] from &ldquo;001<strong>101</strong>&rdquo; forms &ldquo;101&rdquo;</p> </li> </ul> <p>No other selection is valid. Thus, there are 6 total ways.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;11100&rdquo;</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> It can be shown that there are no valid selections.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 <= s.length <= 10<sup>5</sup></code></li> <li><code>s[i]</code> is either <code>'0'</code> or <code>'1'</code>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • numberOfWays

      public long numberOfWays(String s)