Ctrl K

Two-Tone Fence

Easy
A fence is made of boards, each painted white ('0') or black ('1'). The string fence lists the boards from left to right. The owner wants the colours to alternate, so that no two neighbouring boards share a colour. One action repaints a single board in the other colour. Return the minimum number of repaints.
Example 1
fence="00110" → 2

Repainting the second and the fourth boards gives "01010", which takes 2 repaints. Reaching "10101" would take 3.

Example 2
fence="1010" → 0

The colours already alternate, so nothing needs repainting.

Example 3
fence="000" → 1
Constraints
  • 1 ≤ len(fence) ≤ 10⁴
  • fence contains only the characters '0' and '1'
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsValue of a Roman Numeral EasyShared Start of Words EasyBracket Balance EasyWhere the Fragment Starts EasyLength of the Final Word Easy
loading Python…
Input
fence="00110"
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 120 tests on the server, hidden ones included.