
- 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
C++ Program to count number of characters to be removed to get good string
Suppose we have a string S. S contains two types of characters in S, the 'x' and 'a'. We have to count what will be the longest string remaining after removal of few characters in S so that it becomes good string. A string is good if it has strictly more than the half of its length filled with character 'a'.
So, if the input is like S = "xaxxxxa", then the output will be 3, because if we remove 4 'x's, the string will be "xaa" and this is a good string whose length is 3.
Steps
To solve this, we will follow these steps −
x := 2 * count the number of 'a' in S n := size of S return minimum of n and x
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(string S) { int x = 2 * count(S.begin(), S.end(), 'a') - 1; int n = S.size(); return min(n, x); } int main() { string S = "xaxxxxa"; cout << solve(S) << endl; }
Input
"xaxxxxa"
Output
3
- Related Articles
- Python Program to Count Number of Lowercase Characters in a String
- Program to count minimum invalid parenthesis to be removed to make string correct in Python
- What MySQL INSERT() function returns if the number of characters to be removed exceeds the number of characters available in original string?
- Minimum number of elements that should be removed to make the array good using C++.
- Program to find minimum number of intervals to be removed to remove overlaps in C++
- Program to count number of palindromes of size k can be formed from the given string characters in Python
- Program to count number of distinct characters of every substring of a string in Python
- Program to count number of unique palindromes we can make using string characters in Python
- C program to count characters, lines and number of words in a file
- Minimum number of points to be removed to get remaining points on one side of axis using C++.
- Find the number of boxes to be removed in C++
- How to count the number characters in a Java string?
- C++ program to count number of minimum coins needed to get sum k
- C# program to count the number of words in a string
- How to count the number of repeated characters in a Golang String?

Advertisements