Skip to content
Programming101
Programmingoneonone

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
Programmingoneonone

Learn everything about programming

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
  • Automating Image Format Conversion with Python: A Complete Guide
  • 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
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
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes