Source : https://www.hackerrank.com/challenges/ctci-making-anagrams
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
cdeabc
Sample Output
4
Explanation
We delete the following characters from our two strings to turn them into anagrams of each other:
- Remove
d
ande
fromcde
to getc
. - Remove
a
andb
fromabc
to getc
.
We must delete characters to make both strings anagrams, so we print on a new line.
Source : https://www.hackerrank.com/challenges/ctci-making-anagrams
Solution
// Karthikalapati.blogspot.com | |
import java.util.Scanner; | |
public class Solution { | |
private static final int NUM_LETTERS = 26; // we assume lower-case letters only | |
private static int createFilledArray(String first, String second) { | |
/* For each string, create an array of the count of each character */ | |
int [] array1 = createFilledArray(first); | |
int [] array2 = createFilledArray(second); | |
/* Count number of deletions we need to make the Strings anagrams of each other */ | |
int deletions = 0; | |
for (int i = 0; i < NUM_LETTERS; i++) { | |
deletions += Math.abs(array1[i] - array2[i]); | |
} | |
return deletions; | |
} | |
/* Creates an array with the count of each character */ | |
private static int [] createFilledArray(String str) { | |
int [] array = new int[NUM_LETTERS]; | |
for (int i = 0; i < str.length(); i++) { | |
char ch = str.charAt(i); | |
array[ch - 'a']++; | |
} | |
return array; | |
} | |
} | |
// Discuss on HackerRank: https://www.hackerrank.com/challenges/ctci-making-anagrams/forum/comments/246741 |
No comments:
Post a Comment