Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerEarth Largest Balanced String problem solution

YASH PAL, 31 July 2024
In this HackerEarth Largest Balanced String problem solution An empty sequence is balanced.
  • A sequence of the form (A) or [A] or {A} is balanced if A is balanced.
  • A sequence of the form AB is balanced if A and B both are balanced. 
You are given a string A, consisting of ‘(‘, ‘)’, ‘[‘, ‘]’, ‘{‘ and ‘}’ only. You have to find the maximum length of the balanced string after performing some valid operation(s). Valid operations are
  • Remove any character from string A
  • Swap any two adjacent characters of string A
You can perform these valid operations in any order and any numbers of times.
HackerEarth Largest Balanced String problem solution

HackerEarth Largest Balanced String problem solution.

#include <bits/stdc++.h>
using namespace std;
#define MAXN 100005

int main(int argc , char *args[]){
freopen(args[1], "r", stdin);
freopen(args[2], "w", stdout);

int tc;
cin >> tc;
while(tc--){
int cnt[200]={0};
string str;
cin >> str;
for(int i =0 ; i < str.size() ; i ++){
cnt[str[i]]++;
}
long long ans =0 ;
ans += 2*min(cnt['('],cnt[')']);
ans += 2*min(cnt['['],cnt[']']);
ans += 2*min(cnt['{'],cnt['}']);

cout <<ans <<"n";
}
return 0;
}

Second solution

#include<bits/stdc++.h>
using namespace std;

int solve (string str) {
map<char,int>mp;
/*cout<<str<<"n";
cout<<mp['{'];*/
for(int i=0;i<str.size();i++)
{
mp[str[i]]++;
}
int ans=0;
ans+=min(mp['('],mp[')'])*2;
ans+=min(mp['{'],mp['}'])*2;
ans+=min(mp['['],mp[']'])*2;
return ans;
// Write your code here
}

int main() {

ios::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
for(int t_i=0; t_i<T; t_i++)
{
string str;
cin>>str;
//getline(cin, str);

int out_;
out_ = solve(str);
cout << out_;
cout << "n";
}
}
coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes