- 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
Final string after performing given operations in C++
In this tutorial, we are going to solve the following problem.
Given a string containing only characters a and b, our task is to delete the sub-string ab from the string. And print the remaining string.
Here, the idea is very simple to solve the problem. Every string with only a's and b's will shrink to either a's or b's at the end.
Let's see the steps to solve the problem.
Initialize the string.
Initialize two counter variables for a and b.
Iterate over the given string.
Count the a's and b's
Find the maximum from the a and b frequencies.
Print the difference between the two.
Example
Let's see the code.
#include <bits/stdc++.h> using namespace std; string getTheUpdatedString(string str) { int n = str.length(); int a_count = 0, b_count = 0; for (int i = 0; i < n; i++) { if (str[i] == 'a') { a_count++; } else { b_count++; } } string updated_string = ""; if (a_count > b_count) { for (int i = 0; i < a_count - b_count; i++) { updated_string += "a"; } } else { for (int i = 0; i < b_count - a_count; i++) { updated_string += "b"; } } return updated_string; } int main() { string str = "ababababaaa"; cout << getTheUpdatedString(str) << endl; }
Output
If you run the above code, then you will get the following result.
aaa
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- Maximum Possible Product in Array after performing given Operations in C++
- Program to check final answer by performing given stack operations in Python
- Maximum count of equal numbers in an array after performing given operations in C++
- Reduce a number to 1 by performing given operations in C++
- Program to get final string after shifting characters with given number of positions in Python
- Final state of the string after modification in Python
- Performing Bitwise Operations with BigInteger in Java
- Find the longest common prefix between two strings after performing swaps on second string in C++
- Program to find lexicographically smallest string after applying operations in Python
- Program to find expected sum of subarrays of a given array by performing some operations in Python
- Performing mathematical operations in MySQL IF then ELSE is possible?
- Performing power operations on an array of numbers in JavaScript
- Python Program to Create a class performing Calculator Operations
- Program to find maximum score from performing multiplication operations in Python
- C++ code to find minimum stones after all operations

Advertisements