- 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

# Program to count operations to remove consecutive identical bits in Python

Suppose we have a binary string s, now let us consider an operation where we select a bit and flip its value from 0 to 1 or vice-versa. We have to find the minimum number of operations needed to get a string with no three identical consecutive bits.

So, if the input is like s = "10011100", then the output will be 1, because we can flip 1 to 0 the bit at index 4 to make the string "10010100" there are no three consecutive identical bits.

To solve this, we will follow these steps −

- l := 0, count := 0
- while l < size of s, do
- r := l
- while r < size of s and s[r] is same as s[l], do
- r := r + 1

- count := count + floor of ((r - l) / 3)
- l := r

- return count

## Example

Let us see the following implementation to get better understanding −

def solve(s): l = 0 count = 0 while l < len(s): r = l while r < len(s) and s[r] == s[l]: r += 1 count += (r - l) // 3 l = r return count s = "10011100" print(solve(s))

## Input

"10011100"

## Output

1

- Related Questions & Answers
- Python – Consecutive identical elements count
- Python program to count unset bits in a range.
- Python Program to Count set bits in an integer
- Python program to count pairs for consecutive elements
- Check if bits of a number has count of consecutive set bits in increasing order in Python
- Write a python program to count total bits in a number?
- Remove Consecutive Duplicates in Python
- C# program to count total bits in a number
- Java program to count total bits in a number
- Java Program to Count set bits in an integer
- Python program to count total set bits in all number from 1 to n.
- C++ Program to count operations to make filename valid
- C# program to count total set bits in a number
- C/C++ Program to Count set bits in an integer?
- Golang Program to count the set bits in an integer.

Advertisements