#1888
Minimum Number of Flips to Make the Binary String Alternating
specialist · 995 · lc medium +32 · verified · 41.1% accepted · 1,298 likes · top 21%
Description
Given a binary string s, you may perform two operations in any order:
- Type-1: Move s[0] to the end of s.
- Type-2: Flip any single character.
Return the minimum number of Type-2 operations needed to make s alternating (no two adjacent characters equal).
Example 1:
Input: s = "111000"
Output: 2
Explanation: Use the first operation two times to make s = "100011".
Then, use the second operation on the third and sixth elements to make s = "101010".
Example 2:
Input: s = "010"
Output: 0
Explanation: The string is already alternating.
Example 3:
Input: s = "1110"
Output: 1
Explanation: Use the second operation on the second element to make s = "1010".
Code
1
2
3