Number of Ways to Select Buildings

You are given a 0-indexed binary string s which represents the types of buildings along a street where:

  • s[i] = '0' denotes that the i^th building is an office.
  • s[i] = '1' denotes that the i^th building is a restaurant.

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

Return the number of valid ways to select 3 buildings.

Example 1
Inputs = "001101"
Output6
The valid selections form either 010 or 101, and there are 6 total ways.
Example 2
Inputs = "11100"
Output0
It can be shown that there are no valid selections.

Constraints

  • 3 <= s.length <= 10^5
  • s[i] is either '0' or '1'.

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate