- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Deletions of “01” or “10” in binary string to make it free from “01” or “10” in C++ Program
In this tutorial, we are going to write a program that finds the total number of pairs (01 or 10) to free the binary string from the pairs (01 and 10). Let's see an example.
Input − 101010001
Output − 4
In the above example, we have to delete a total of 4 pairs to free the binary string from the pairs (01 and 10).
The resultant string after deleting all the pairs is 0.
We have to delete all the 01 and 10 pairs from the binary string. So, the total number of pairs to be deleted is the minimum of count(1) and count(0).
Let's see the steps to solve the problem.
Initialize the binary string.
Find the zeroes and ones count.
Print the minimum from the zeroes and ones count.
Example
Let's see the code.
#include <bits/stdc++.h> using namespace std; int findMinimumNumberOfDeletions(string str, int len) { int zeroes_count = 0, ones_count = 0; // counting zeroes and ones for (int i = 0; i < len; i++) { if (str[i] == '0') { zeroes_count++; } else { ones_count++; } } return min(zeroes_count, ones_count); } int main() { string str = "101010001"; int len = str.length(); cout << findMinimumNumberOfDeletions(str, len) << endl; return 0; }
Output
If you execute the above program, then you will get the following result.
4
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- Deletions of “01” or “10” in binary string to make it free from “01” or “10" in C++?
- Program to find how max score we can get by removing 10 or 01 from binary string in Python
- 01 Matrix in C++
- Count of operations to make a binary string “ab” free in C++
- C Program to build DFA accepting the languages ending with “01”
- Program to find minimum deletions to make string balanced in Python
- Minimum number of deletions to make a string palindrome in C++.
- Format hour in kk (01-24) format in Java
- Display Seconds in ss format (01, 02) in Java
- Java Program to format date as Apr 14 2019 01:35 PM IST
- Program to sqeeze list elements from left or right to make it single element in Python
- Order dates in MySQL with the format “01 August 2019”?
- Program to check we can replace characters to make a string to another string or not in C++
- Program to find minimum number of deletions required from two ends to make list balanced in Python
- Program to find minimum deletions to make strings strings in Python
