
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find minimum changes required for alternating binary string in Python
Suppose we have a binary string s. Let us consider an operation where we can flip one bit. The string s is called alternating string if no two adjacent characters are same. We have to find the minimum number of operations needed to make s alternating.
So, if the input is like s = "11100011", then the output will be 3 because if we flip bits at position 1, 4 and 7, then, it will be "10101010", then all are alternating.
To solve this, we will follow these steps −
change := 0
even_1 := 0, even_0 := 0
odd_1 := 0, odd_0 := 0
for i in range 0 to size of s - 1, do
if i is even, then
if s[i] is same as '1', then
even_1 := even_1 + 1
otherwise,
even_0 := even_0 + 1
otherwise,
if s[i] is same as '1', then
odd_1 := odd_1 + 1
otherwise,
odd_0 := odd_0 + 1
if (even_1+odd_0) > (even_0+odd_1), then
change := even_0 + odd_1
otherwise,
change := even_1 + odd_0
return change
Example (Python)
Let us see the following implementation to get better understanding &minnus;
def solve(s): change = 0 even_1 = 0 even_0 = 0 odd_1 = 0 odd_0 = 0 for i in range(len(s)): if(i%2 == 0): if(s[i]=='1'): even_1 +=1 else: even_0 +=1 else: if(s[i] == '1'): odd_1 +=1 else: odd_0 +=1 if((even_1+odd_0)>(even_0+odd_1)): change = even_0 + odd_1 else: change = even_1 + odd_0 return change s = "11100011" print(solve(s))
Input
"11100011"
Output
3
- Related Articles
- Minimum swaps required to make a binary string alternating in C++
- Program to find minimum number of flips required to have alternating values in Python
- Program to find minimum space plane required for skydivers in k days in python
- Program to find minimum required chances to form a string with K unique characters in Python
- Program to find length of longest alternating path of a binary tree in python
- Program to find minimum number of operations required to make one string substring of other in Python
- Minimum characters required to be removed to sort binary string in ascending order
- Program to find minimum swaps required to make given anagram in python
- Program to find minimum remove required to make valid parentheses in Python
- Program to traverse binary tree level wise in alternating way in Python
- Minimum number of operations required to sum to binary string S using C++.
- Python Program To Get Minimum Element For String Construction
- 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
- C++ program to find minimum how many coins needed to buy binary string
