← Back to challenges

Alternating Ones and Zeroes

PythonHardstringsvalidationlogic

Instructions

Write a function that returns True if the binary string can be rearranged to form a string of alternating 0s and 1s.

Examples

can_alternate("0001111") ➞ True
# Can make: "1010101"

can_alternate("01001") ➞ True
# Can make: "01010"

can_alternate("010001") ➞ False

can_alternate("1111") ➞ False

Notes

  • No substring of the output may contain more than one consecutive repeating character (e.g. 00 or 11 are not allowed).
  • Return False if a string only contains 0s or only contains 1s.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Vowel Sandwich