
- 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
Maximum number of removals of given subsequence from a string in C++
Given the task is to find the maximum number of removals of given subsequence from a string. A string s is given and we have to find the maximum number of subsequence ‘abc’ that can be removed from the string.
Let’s now understand what we have to do using an example:
Input
s = ‘dnabcxy’
Output
1
Explanation − Only one subsequence of ‘abc’ can be found in the given string (‘dnabcxy’), therefore the output is 1.
Input
s = ‘zcabcxabc’
Output
2 (‘zcabcxabc’)
Approach used in the below program as follows
In Max() function initialize variables i, a, ab, abc with value = 0 and of type int.
Loop from i=0 till I < s.length()
Inside the loop check if (s[i] == ‘a’), if so then increment value of a.
Else, check if (s[i] == ‘b’), if true then again check if (a > 0). If both conditions are true then decrease value of a by 1 and increment the value of ab.
Finally, check if (s[i] == ‘c’), if true then again check if (ab > 0). If both conditions are true then decrease value of ab by 1 and increment the value of abc.
Return abc
Example
#include <bits/stdc++.h> using namespace std; int Max(string s){ int i=0, a=0, ab=0, abc=0; for (i = 0; i < s.length(); i++){ if (s[i] == 'a'){ a++; } else if (s[i] == 'b'){ if (a > 0){ a--; ab++; } } else if (s[i] == 'c'){ if (ab > 0){ ab--; abc++; } } } return abc; } //main function int main(){ string s = "zcabcxabc"; cout << Max(s); return 0; }
Output
2
- Related Articles
- Count subsequence of length three in a given string in C++
- Program to Find Out the Maximum Points From Removals in Python
- Find a palindromic string B such that given String A is a subsequence of B in C++
- Maximum weight transformation of a given string in C++
- Maximum sum increasing subsequence from a prefix and a given element after prefix is must in C++
- Count all Palindromic Subsequence in a given String in C++
- Maximum product of subsequence of size k in C++
- Maximum product of an increasing subsequence in C++
- Maximum Number of Vowels in a Substring of Given Length in C++
- Program to find length of longest Fibonacci subsequence from a given list in Python
- Program to find length of longest alternating subsequence from a given list in Python
- Maximum product of a triplet (subsequence of size 3) in array in C++
- Maximum product of an increasing subsequence in C++ Program
- Maximum product of a triplet (subsequence of size 3) in array in C++ Program.
- Subsequence of size k with maximum possible GCD
