Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Bob and String problem solution

YASH PAL, 31 July 202414 February 2026
In this HackerEarth Bob and String problem solution, Bob and Khatu both love the string. Bob has a string S and Khatu has a string T. They want to make both string S and T to anagrams of each other. Khatu can apply two operations to convert string T to an anagram of string S which are given below:
  1. Delete one character from the string T.
  2. Add one character from the string S.
Khatu can apply above both operations as many times as he wants. Find the minimum number of operations required to convert string T so that both T and S will become anagram of each other.
 
 
HackerEarth Bob and String problem solution

 

 

HackerEarth Bob and String problem solution.

#include <bits/stdc++.h>
using namespace std;
int main()
{
int test;
cin>>test;
while(test--)
{
string S,T;
cin>>S>>T;
int hashS[26]={0};
int hashT[26]={0};
for(int i=0;i<S.size();i++)
hashS[S[i]-'a']++;

for(int i=0;i<T.size();i++)
hashT[T[i]-'a']++;

int count=0;
for(int i=0;i<26;i++)
{
if(hashS[i]-hashT[i]>0)
count+=hashS[i]-hashT[i];
if(hashS[i]-hashT[i]<0)
count+=hashT[i]-hashS[i];
}
cout<<count<<endl;
}
return 0;
}
 

Second solution

#include<bits/stdc++.h>

using namespace std;

#define vi vector < int >
#define pii pair < int , int >
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define foreach(it,v) for( __typeof((v).begin())it = (v).begin() ; it != (v).end() ; it++ )
#define ll long long
#define llu unsigned long long
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define dbg(x) { cout<< #x << ": " << (x) << endl; }
#define dbg2(x,y) { cout<< #x << ": " << (x) << " , " << #y << ": " << (y) << endl; }
#define all(x) x.begin(),x.end()
#define mset(x,v) memset(x, v, sizeof(x))
#define sz(x) (int)x.size()

int main()
{
int t;
cin >> t;
assert(1 <= t && t <= 10);
while(t--)
{
string a , b;
cin >> a >> b;
int n = sz(a) , m = sz(b) , i;
assert(1 <= n && n <= 100000);
assert(1 <= m && m <= 100000);
int c[256] = {0};
for(i=0;i<n;i++)
{
c[a[i]]++;
}
for(i=0;i<m;i++)
{
c[b[i]]--;
}
int ans = 0;
for(i=0;i<256;i++)
{
ans += abs(c[i]);
}
cout << ans << endl;
}
return 0;
}
 
coding problems solutions HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes