-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathSolution.java
More file actions
30 lines (25 loc) · 1.04 KB
/
Solution.java
File metadata and controls
30 lines (25 loc) · 1.04 KB
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
import java.util.Scanner;
public class Solution {
private static boolean isAnagram(String a, String b) {
return countOfCharacters(a.toLowerCase()).equals(countOfCharacters(b.toLowerCase()));
}
// HACKHACK
// HackerRank is preventing me from modifying the template solution that
// only imports import java.util.Scanner. If this limitation were removed,
// use `Map` and `HashMap` below and extract imports to the top of the file.
private static java.util.Map<Character, Integer> countOfCharacters(String word) {
java.util.Map<Character, Integer> counts = new java.util.HashMap<>();
for(char c : word.toCharArray()) {
counts.put(c, 1 + counts.getOrDefault(c, 0));
}
return counts;
}
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" );
}
}