Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank, Algorithm Solutions of Problem Solving Section in Java. At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.
In this post, you will find the solution for Making Anagrams in Java-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.
Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.
Introduction To Algorithm
The word Algorithm means “a process or set of rules to be followed in calculations or other problem-solving operations”. Therefore Algorithm refers to a set of rules/instructions that step-by-step define how a work is to be executed upon in order to get the expected results.
Advantages of Algorithms:
- It is easy to understand.
- Algorithm is a step-wise representation of a solution to a given problem.
- In Algorithm the problem is broken down into smaller pieces or steps hence, it is easier for the programmer to convert it into an actual program.
Link for the Problem – Making Anagrams – Hacker Rank Solution
Making Anagrams– Hacker Rank Solution
Problem:
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.
Example.
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.
makingAnagrams has the following parameter(s):
- string s1: a string
- string s2: a string
Returns
- int: the minimum number of deletions needed
Input Format
The first 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].
Sample Input
cde abc
Sample Output
4
Explanation
Delete the following characters from our two strings to turn them into anagrams:
- Remove
d
ande
fromcde
to getc
. - Remove
a
andb
fromabc
to getc
.
characters have to be deleted to make both strings anagram
Making Anagrams – Hacker Rank Solution
import java.util.HashMap; import java.util.Scanner; /** * @author Techno-RJ * */ public class MakingAnagrams { public static int numberNeeded(String first, String second) { HashMap<Character, Integer> hmap = new HashMap<Character, Integer>(); int length = 0; for (int i = 0; i < first.length(); i++) { char c = first.charAt(i); if (hmap.containsKey(c)) { hmap.put(c, hmap.get(c) + 1); } else { hmap.put(c, 1); } } for (int i = 0; i < second.length(); i++) { char c = second.charAt(i); if (hmap.containsKey(c) && hmap.get(c) != 0) { hmap.put(c, hmap.get(c) - 1); length += 2; } } return (first.length() + second.length() - length); } public static void main(String[] args) { Scanner in = new Scanner(System.in); String a = in.next(); String b = in.next(); in.close(); System.out.println(numberNeeded(a, b)); } }