2017-02-12 00:31:12 -08:00
|
|
|
// Author: Rodney Shaghoulian
|
|
|
|
|
// Github: github.com/rshaghoulian
|
|
|
|
|
// HackerRank: hackerrank.com/rshaghoulian
|
|
|
|
|
|
2017-02-07 01:14:49 -08:00
|
|
|
import java.io.*;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
public class Solution {
|
|
|
|
|
/* Runtime: O(n) using a HashMap */
|
|
|
|
|
static boolean isAnagram(String a, String b) {
|
|
|
|
|
if (a == null || b == null || a.length() != b.length()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
a = a.toLowerCase();
|
|
|
|
|
b = b.toLowerCase();
|
|
|
|
|
|
|
|
|
|
HashMap<Character, Integer> map = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
/* Fill HashMap with 1st String */
|
|
|
|
|
for (int i = 0; i < a.length(); i++) {
|
|
|
|
|
char ch = a.charAt(i);
|
|
|
|
|
if (map.containsKey(ch)) {
|
|
|
|
|
map.put(ch, map.get(ch) + 1);
|
|
|
|
|
} else {
|
|
|
|
|
map.put(ch, 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Compare 2nd String to 1st String's HashMap */
|
|
|
|
|
for (int i = 0; i < b.length(); i++) {
|
|
|
|
|
char ch = b.charAt(i);
|
|
|
|
|
if (map.containsKey(ch) && map.get(ch) > 0) {
|
|
|
|
|
map.put(ch, map.get(ch) - 1);
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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" );
|
|
|
|
|
}
|
|
|
|
|
}
|