
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Deletions of “01†or “10†in binary string to make it free from “01†or “10" in C++?
Let us first declare our initial string and calculate its length and pass them to the deleteSubstr(str,length) function.
string str = "01010110011"; int length = str.length(); cout <<"Count of substring deletion"<< deleteSubstr(str, length);
Inside of the deleteSubstr(string str, int length) function the for loop runs till I is less than length and increments the count_0 and count_1 variable on encounter of 0 and 1 respectively. The function then returns the min value of the count_0 and count_1.
int deleteSubstr(string str, int length){ int count_0 = 0, count_1 = 0; for (int i = 0; i < length; i++) { if (str[i] == '0') count_0++; else count_1++; } return min(count_0, count_1); }
Example
Let us see the following implementation of deletions of “01” or “10” in binary string to make it free from “01” or “10”−
#include <iostream> using namespace std; int deleteSubstr(string str, int length){ int count_0 = 0, count_1 = 0; for (int i = 0; i < length; i++) { if (str[i] == '0') count_0++; else count_1++; } return min(count_0, count_1); } int main(){ string str = "01010110011"; int length = str.length(); cout <<"Count of substring deletion "<< deleteSubstr(str, length); return 0; }
Output
The above code will produce the following output −
Count of substring deletion 5
- Related Articles
- Deletions of “01†or “10†in binary string to make it free from “01†or “10†in C++ Program
- 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++
- Format hour in kk (01-24) format in Java
- Display Seconds in ss format (01, 02) in Java
- Minimum number of deletions to make a string palindrome in C++.
- Program to find minimum deletions to make string balanced in Python
- Order dates in MySQL with the format “01 August 2019�
- Display hour in hh (01-12 in AM/PM) format in Java
- Top 10 Free Android Games
- C Program to build DFA accepting the languages ending with “01â€
- MySQL search results by month in format 2015-07-01 11:15:30?
- Local Variable Type Inference or LVTI in Java 10
- Upgrade your computer for free to windows 10

Advertisements