
- 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 length of balanced string after swapping and removal of characters in C++
We are given a string containing (,),{,},[,] characters only. The goal is to find the maximum length of such string so that it becomes balanced by swapping adjacent characters or removing a character. We will do this by comparing adjacent characters, if they are opposite of each other then they can be swapped. ( }{,)(,][ can be swapped, while {{,)),[[,}},)),]] cannot be swapped ).
Also if a character has no matching pair, then it can be removed also. ( “{{}][“, here first { can be removed and balanced string length becomes 4 )
Input
str[]= “ {{{}}{]]][()” length 12
Output
Maximum length of balances string: 8
Explanation − str[0] and str[1] cannot be swapped, remove str[0]= “{{}}{]]][()” Original str[1] and str[2] cannot be swapped, remove str[1]= “{}}{]]][()” {} are balanced }{ can be swapped, remove next 2 ]], swap ][ and () are also balanced Final string is {}{}[](). Length is 8.
Input
str[]= “(((((()” length 7
Output
str[]= “(((((()” length 7
Explanation − Only str[5] and str[6] are balanced, remove all. Final string (). Length is 2
Approach used in the below program is as follows
Character array str[] stores the original string. Integer Length stores the length of the string.
Function maxBalancedStr (char str[], int len) takes the string and its length as parameters and returns the maximum length of the balanced string.
Variable count is used to store the length of such string, initially 0.
Start traversing the string from the first character and check if adjacent character can be swapped to make both of them balanced. Or if they are already balanced, increase count by 2.
Do this for pairs like, (),)( and {},}{ and [],][, increment i also if such pairs exist, to move to the next character.
In the end the count stores the length of the balanced string.
Return count as result.
Example
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the length of // the longest balanced sub-string int maxBalancedStr(char str[20], int len){ int count=0; // Traversing the string for (int i = 0; i <len;i++) { // Check type of parentheses and // incrementing count for it if((str[i]=='(' && str[i+1]==')') || (str[i]==')' && str[i+1]=='(')) //can swap{ count+=2; ++i; } else if((str[i]=='{' && str[i+1]=='}') || (str[i]=='}' && str[i+1]=='{')) //can swap{ count+=2; ++i; } else if((str[i]=='[' && str[i+1]==']') || (str[i]==']' && str[i+1]=='[')) //can swap count+=2; ++i; } } return count; } // Driven code int main(){ char str[] = ")([]](("; int length=7; cout << maxBalancedStr(str,length); return 0; }
Output
4
- Related Articles
- Swapping Characters of a String in Java
- Swapping Characters of a String in C#
- Maximum Length of a Concatenated String with Unique Characters in C++
- Program to equal two strings of same length by swapping characters in Python
- C++ Program to find array after removal from maximum
- Program to find length of longest contiguously strictly increasing sublist after removal in Python
- C++ Program to Swapping Pair of Characters
- Golang program to swapping pair of characters
- Maximum count of substrings of length K consisting of same characters in C++
- What is the maximum length of string in Python?
- Program to find length of concatenated string of unique characters in Python?
- Swapping adjacent words of a String in JavaScript
- Program to find min length of run-length encoding after removing at most k characters in Python
- Maximum removal from array when removal time >= waiting time in C++
- Program to find maximum score of brick removal game in Python
