-
Notifications
You must be signed in to change notification settings - Fork 861
/
StringsMakingAnagrams.java
46 lines (42 loc) · 1.12 KB
/
StringsMakingAnagrams.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
*
* Problem Statement-
* [Strings: Making Anagrams](https://www.hackerrank.com/challenges/ctci-making-anagrams/problem)
*
*/
package com.javaaid.hackerrank.solutions.algorithms.strings;
import java.util.HashMap;
import java.util.Scanner;
/**
* @author Kanahaiya Gupta
*
*/
public class StringsMakingAnagrams {
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));
}
}