Header Ad

HackerRank Strings: Making Anagrams problem solution

HackerRank Strings: Making Anagrams Interview preparation kit problem you have Given two strings, a and b, that may or may not be of the same length, determine the minimum number of character deletions required to make a and b anagrams. Any characters can be deleted from either of the strings.


HackerRank Strings: Making Anagrams problem solution

Problem solution in Python programming.

import math

def number_needed(a, b):
    aString = [0]*26
    for ch in a:
        aString[ord(ch)-97] += 1
    
    bString = [0]*26
    for ch in b:
        bString[ord(ch)-97] += 1
        
    deletions = 0
    for i in range(len(aString)):
        deletions += math.fabs(aString[i]-bString[i])
    
    return int(deletions)

a = input().strip()
b = input().strip()

print(number_needed(a, b))



Problem solution in Java Programming.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
   
     public static int numberNeeded(String first, String second) {
        Map<Character, Integer> map = new HashMap<>();
        int count = 0;
        for(int i=0;i<first.length();i++){
            if(map.get(first.charAt(i)) == null){
                map.put(first.charAt(i), 1);
            } else {
                int cur = map.get(first.charAt(i));
                map.put(first.charAt(i), cur+1);
            }
        }
        for(int i=0;i<second.length();i++){
            if(map.containsKey(second.charAt(i))){
                int cur = map.get(second.charAt(i));
                if(cur == 1){
                   map.remove(second.charAt(i)); 
                } else {
                    map.put(second.charAt(i), cur-1);
                }
            } else {
                count++;
            }
        }
         
        for(Integer i: map.values()){
            count=count+i;
        }
        
        return count;
     }
  
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String a = in.next();
        String b = in.next();
        System.out.println(numberNeeded(a, b));
    }
}


Problem solution in C++ programming.

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>

using namespace std;

int number_needed(string a, string b) {
    int i,c[26]={0},c1[26]={};
    for(i=0;i<a.length();i++)
        {
        if(97<=a[i]&&a[i]<=123)
            c[a[i]-97]++;
    }
    for(i=0;i<b.length();i++)
        {
        if(97<=b[i]&&b[i]<=123)
            c1[b[i]-97]++;
    } 
    int s=0;
    for(i=0;i<26;i++)
    {
        s=s+abs(c[i]-c1[i]);
        
    }   
    
    return (s);
    
}

int main(){
    string a;
    cin >> a;
    string b;
    cin >> b;
    cout << number_needed(a, b) << endl;
    return 0;
}


Problem solution in C programming.

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <math.h>

int main()
{
#ifdef _DEBUG
	char FNAME[250];
	strcpy(FNAME, __FILE__);
	strcpy(strchr(FNAME, '.'), ".txt");
	freopen(FNAME, "rt", stdin);
#endif

	char a[10001], b[10001];
	scanf("%s %s", a, b);

	char abc['z' + 1];
	memset(abc, 0, sizeof abc);

	for (char *ptr = a; *ptr != '\0'; ++ptr)
		abc[*ptr]++;

	for (char *ptr = b; *ptr != '\0'; ++ptr)
		abc[*ptr]--;

	int changes = 0;

	for (int c = 'a'; c <= 'z'; ++c)
		changes += abs(abc[c]);

	printf("%d", changes);

}


Problem solution in JavaScript programming.

process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
    input_stdin += data;
});

process.stdin.on('end', function () {
    input_stdin_array = input_stdin.split("\n");
    main();    
});

function readLine() {
    return input_stdin_array[input_currentline++];
}

/////////////// ignore above this line ////////////////////

function main() {
    var a = readLine();
    var b = readLine();
    var aCount = getCharCount(a);
    var bCount = getCharCount(b);
    var deleteCount = 0;
    for(var i = 0; i < aCount.length; i++) {
        var min = Math.min(aCount[i], bCount[i]);
        var max = Math.max(aCount[i], bCount[i]);
        deleteCount += (max - min)
    }
    
    console.log(deleteCount);
}

function getCharCount(str) {
    var count = new Array(26);
    count.fill(0);
    for(var i = 0; i < str.length; i++) {
        var index = str[i].charCodeAt(0) - 97;
        count[index] = count[index] + 1;
    }
    
    return count;
}


Post a Comment

0 Comments