forked from sknsht/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
35 lines (28 loc) · 940 Bytes
/
Solution.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
import java.util.*;
public class Solution {
static boolean isAnagram(String a, String b) {
if (a.length() != b.length())
return false;
a = a.toLowerCase();
b = b.toLowerCase();
HashMap<Character, Integer> map = new HashMap<>();
for (char letter : a.toCharArray()) {
map.put(letter, map.getOrDefault(letter, 0) + 1);
}
for (char letter : b.toCharArray()) {
if (!map.containsKey(letter) || map.get(letter) == 0)
return false;
else
map.put(letter, map.get(letter) - 1);
}
return true;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String a = scan.next();
String b = scan.next();
scan.close();
boolean ret = isAnagram(a, b);
System.out.println((ret) ? "Anagrams" : "Not Anagrams");
}
}