Skip to the code
cf.
+10 XP

#287 Complete the Toggle Row

easy

A settings page shows a row of toggles. The design rule says two toggles next to each other can't both be on. An admin has locked some toggles, and the rest are free.

pattern describes the row: "1" is locked on, "0" is locked off, and "?" is free. Write completeTogglePatterns(pattern) that returns every way to set the free toggles so the whole row follows the rule, as strings of "0" and "1".

Return the strings sorted ascending. If no setting works (for example, two locked toggles next to each other are both on), return [].

Example 1

Input: pattern = "???"
Output: ["000","001","010","100","101"]

Every 3-toggle row with no two neighbors on, from smallest to largest.

Example 2

Input: pattern = "1?0?"
Output: ["1000","1001"]

The second toggle must be off because the first is on. The last one can be either.

Constraints

  • 1 <= pattern.length <= 12
  • pattern only contains 0, 1, and ?.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).