-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Solution.java
43 lines (38 loc) · 1.24 KB
/
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
36
37
38
39
40
41
42
43
// github.com/RodneyShag
import java.io.*;
import java.util.*;
public class Solution {
// Time Complexity: O(n) using a HashMap
// Space Complexity: O(n)
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);
map.merge(ch, 1, Integer::sum);
}
/* 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" );
}
}