- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a binary string s. Now suppose we can take some prefix of s and move it to the back. then, find the minimum number of characters that need to be flipped such that there will be no consecutive characters of the same value.

So, if the input is like s = "10010101111", then the output will be 2, as we can take prefix "10", then move it to back so string is "01010111110" then flip 3rd and 5th bit from right to 0 ("01010101010").

To solve this, we will follow these steps −

ans := size of S

N := size of S

s := 0

for i in range 0 to 2 * N, do

s := s + integer of (S[i mod N] XOR (i AND 1))

if i >= N - 1, then

ans := minimum of ans, s and N - s

s := s - integer of (S[(i -(N - 1)) mod N]) XOR ((i - (N - 1)) AND 1)

return ans

Let us see the following implementation to get a better understanding −

class Solution: def solve(self, S): ans = N = len(S) s = 0 for i in range(2 * N): s += int(S[i % N]) ^ (i & 1) if i >= N - 1: ans = min(ans, s, N - s) s -= int(S[(i - (N - 1)) % N]) ^ ((i - (N - 1)) & 1) return ans ob = Solution() s = "10010101111" print(ob.solve(s))

"10010101111"

2

- Related Questions & Answers
- Program to find minimum changes required for alternating binary string in Python
- Program to find minimum number of operations required to make one number to another in Python
- Program to find minimum number of hops required to reach end position in Python
- Program to find minimum number of buses required to reach final target in python
- Minimum flips required to maximize a number with k set bits in C++.
- Program to find minimum number of operations required to make lists strictly Increasing in python
- Program to find minimum number of movie theatres required to show all movies in python
- Program to find minimum number of busses are required to pass through all stops in Python
- Program to find minimum number of operations required to make one string substring of other in Python
- Program to find minimum number of bricks required to make k towers of same height in Python
- Program to find number of columns flips to get maximum number of equal Rows in Python?
- Program to find minimum number of pins required to hang all banners in C++
- Program to find minimum number of steps required to catch the opponent in C++
- Program to find minimum number of deletions required from two ends to make list balanced in Python
- Program to find number of minimum steps required to meet all person at any cell in Python

Advertisements