Making Anagrams - HackerRank Solution.
NOTE: If you are copying my code then its an advice to you to copy it after downloading it to avoid any kind of compilation error its link is available at the bottom of the source code.
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 is taking a cryptography class and finding anagrams to be very useful. She 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 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, and . The only characters that match are the 's so we have to remove from and from for a total of deletions.
Function Description
Complete the makingAnagrams function in the editor below. It should return an integer representing the minimum number of deletions needed to make the strings anagrams.
makingAnagrams has the following parameter(s):
- s1: a string
- s2: a string
Input Format
The first line contains a single string, .
The second line contains a single string, .
The second line contains a single string, .
Constraints
- It is guaranteed that and consist of lowercase English letters, ascii[a-z].
Output Format
Print a single integer denoting the minimum number of characters which must be deleted 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:
- Remove
d
ande
fromcde
to getc
. - Remove
a
andb
fromabc
to getc
.
We had to delete characters to make both strings anagrams.
SOURCE CODE
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
// Complete the makingAnagrams function below.
static int makingAnagrams(String s1, String s2) {
int count=0;
for(char ch='a';ch<='z';ch++)
{
int freq1=0,freq2=0;
for(int i=0;i<s1.length();i++)
{
if(ch==s1.charAt(i))
freq1++;
}
for(int i=0;i<s2.length();i++)
{
if(ch==s2.charAt(i))
freq2++;
}
count+=Math.abs(freq1-freq2);
}
return(count);
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String s1 = scanner.nextLine();
String s2 = scanner.nextLine();
int result = makingAnagrams(s1, s2);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedWriter.close();
scanner.close();
}
}
Click here to Download
RESULT
If you have any question then leave a comment below I will do my best to answer that question.
Comments
Post a Comment