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. 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 data structure