Sunday, February 10, 2019

Strings: Making Anagrams - Hacker Rank Solution

Check out the resources on the page's right side to learn more about strings. The video tutorial is by Gayle Laakmann McDowell, author of the best-selling interview book Cracking the Coding Interview.

Alice is taking a cryptography class and finding anagrams to be very useful. We consider two strings to be anagrams of each other if the first string's letters can be rearranged to form the second string. In other words, both strings must contain the same exact letters in the same exact frequency For example, bacdc and dcbac are anagrams, but bacdc and dcbad are not.
Alice decides on an encryption scheme involving two large strings where encryption is dependent on the minimum number of character deletions required to make the two strings anagrams. Can you help her find this number?
Given two strings,  and , that may or may not be of the same length, determine the minimum number of character deletions required to make  and  anagrams. Any characters can be deleted from either of the strings.
For example, if  and , we can delete  from string  and  from string  so that both remaining strings are and  which are anagrams.
Function Description
Complete the makeAnagram function in the editor below. It must return an integer representing the minimum total characters that must be deleted to make the strings anagrams.
makeAnagram has the following parameter(s):
  • a: a string
  • b: a string
Input Format
The first line contains a single string, .
The second line contains a single string, .
Constraints
  • The strings  and  consist of lowercase English alphabetic letters ascii[a-z].
Output Format
Print a single integer denoting the number of characters you must delete to make the two strings anagrams of each other.
Sample Input
cde
abc
Sample Output
4
Explanation
We delete the following characters from our two strings to turn them into anagrams of each other:
  1. Remove d and e from cde to get c.
  2. Remove a and b from abc to get c.
We must delete  characters to make both strings anagrams, so we print  on a new line.

Strings: Making Anagrams - Hacker Rank Solution

Two strings,  and , will be anagrams of one another if they share all of the same characters and each character has the same frequency in both strings. Keep a count array for each string that stores the number of occurrences of each of character. Suppose character  occurs  times in string  and  times in string ; in this case, we'll have to perform  deletions for all of the characters.
Featured Solutions

Python 2

from collections import *    
a = Counter(raw_input())
b = Counter(raw_input())
c = a - b
d = b - a
e = c + d
print len(list(e.elements()))
Problem Setter's code:
#include<bits/stdc++.h>

using namespace std;

int main() {
    string str1,str2;
    getline(cin,str1);
    getline(cin,str2);

    int A[26],B[26],i;
    for(i=0 ; i< 26 ; i++)
        A[i] = B[i] = 0;
    for(i = 0 ; i< str1.length() ; i++)
        A[(int)(str1[i] - 'a')]++;
    for(i = 0 ; i< str2.length() ; i++)
        B[(int)(str2[i] - 'a')]++; 
    int outp = 0;
    for(i=0 ; i< 26 ; i++)
    {
        outp = outp + A[i] + B[i] - 2*min(A[i],B[i]);
    }
    cout<<outp<<endl;
    return 0;
}
Problem Tester's code:

Haskell

import Data.List
main :: IO ()
main = getContents >>= print. parse
parse :: String -> Int
parse input =
  case lines input of
    [a, b] -> validate (a, b)
    _      -> error "there are not two lines"
validate :: (String, String) -> Int
validate (aa, bb)
  | length aa < 1 || length aa > 10000 = error "length of a is out of range"
  | length bb < 1 || length bb > 10000 = error "length of b is out of range"
  | otherwise = foldr (\(p, q) acc -> abs(p-q) + acc) 0 $ zip a b
    where
      a = map (length). group. sort $ aa ++ ['a'..'z']
      b = map (length). group. sort $ bb ++ ['a'..'z']

No comments:

Post a Comment

Powered by Blogger.