Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • 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
Programmingoneonone
Programmingoneonone

HackerRank Making Anagrams problem solution

YASH PAL, 31 July 2024

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;
}

{“mode”:”full”,”isActive”:false}

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;
}

{“mode”:”full”,”isActive”:false}

coding problems solutions

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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