Talentd
FleetCode

3750. Minimum Number of Flips to Reverse Binary String

Easy75.5% acceptance

You are given a positive integer n.

Let s be the binary representation of n without leading zeros.

The reverse of a binary string s is obtained by writing the characters of s in the opposite order.

You may flip any bit in s (change 0 → 1 or 1 → 0). Each flip affects exactly one bit.

Return the minimum number of flips required to make s equal to the reverse of its original form.

Example 1:

Input: n = 7

Output: 0

Explanation:

The binary representation of 7 is "111". Its reverse is also "111", which is the same. Hence, no flips are needed.

Example 2:

Input: n = 10

Output: 4

Explanation:

The binary representation of 10 is "1010". Its reverse is "0101". All four bits must be flipped to make them equal. Thus, the minimum number of flips required is 4.

Constraints:

  • 1 <= n <= 109

Hints open one at a time

Hint 1
Generate the reverse of the binary string and use two pointers from the ends to determine where flips are needed.
3:24
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def minimumFlips(self, n: int) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
7
Input
10
Your code is saved on this device.Solution