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 Bob and String problem solution

YASH PAL, 31 July 2024
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

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
©2025 Programming101 | WordPress Theme by SuperbThemes