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

HackerRank Making Anagrams problem solution

YASH PAL, 31 July 202410 February 2026

In this HackerRank Making Anagrams problem solution, we have given two strings, s1 and s2, that may not be of the same length, to determine the minimum number of character deletions required to make s1 and s2 anagrams. Any characters can be deleted from either of the strings.

HackerRank Making Anagrams problem solution

 

Problem solution in Python.

first = sorted(list(input("")))
firstList = sorted(list(first))
second = sorted(list(input("")))
secondList = sorted(list(second))
last_first = 0
last_second = 0
deletion = 0

for i in range(0, len(first)):
    if(first[i] not in secondList):
        deletion += 1
    else:
        for x in range(last_second, len(second)):
            if(secondList[x] == first[i]):
                secondList[x] = ""
                last_second = x
                break
        
for j in range(0, len(second)):
    if(second[j] not in firstList):
        deletion += 1
    else:
        for y in range(last_first, len(first)):
            print
            if(firstList[y] == second[j]):
                firstList[y] = ""
                last_first = y
                break
                
print(deletion)


Problem solution in Java.

import java.util.Scanner;

public class Solution {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		String str1 = sc.next();
		String str2 = sc.next();

		int[] str1C = new int[26];
		int[] str2C = new int[26];

		for (char c : str1.toCharArray()) {
			int ascii = (int) c;
			int index = ascii - 97;
			str1C[index] = str1C[index] + 1;
		}

		for (char c : str2.toCharArray()) {
			int ascii = (int) c;
			int index = ascii - 97;
			str2C[index] = str2C[index] + 1;
		}

		int deletions = 0;
		for (int i = 0; i < 26; i++) {
			deletions += Math.abs(str1C[i] - str2C[i]);
		}
		System.out.println(deletions);

		sc.close();
	}
}

 

Problem solution in C++.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int cnt1[ 26 ], cnt2[ 26 ]; 
string a, b;

int main() {
    cin >> a >> b;
    for ( int i = 0; i < a.size(); i++ ) cnt1[ a[i] - 'a' ]++;
    for ( int i = 0; i < b.size(); i++ ) cnt2[ b[i] - 'a' ]++;
    int answer = 0;
    for ( int i = 0; i < 26; i++ ) answer += max( cnt1[i], cnt2[i] ) - min( cnt1[i], cnt2[i] );
    cout << answer << "n";
    return 0;
}

 

 

Problem solution in C.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int abs(int x)
{
    if(x>0)
        return x;
    else
        return -x;
}
int main() {
    
    int A[26],B[26];
    char str1[10000],str2[10000];
    int i,count;
    count = 0;
    for(i=0;i<26;i++)
        A[i] = B[i] = 0;
        
    
    scanf("%s",str1);
    scanf("%s",str2);
    
    i=0;
    while(str1[i]!= '')
        {
        A[str1[i]-97]++;
        i++; 
        }
    
    i=0; 
    while(str2[i]!= '')
        {
        B[str2[i]-97]++;
        i++; 
        }
    
    
    for(i=0;i<26;i++)
        count = count+ abs(A[i]-B[i]);
    
    printf("%d",count);
    
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    return 0;
}

 

 

coding problems solutions Hackerrank Problems Solutions HackerRank

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