-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnagram.java
More file actions
61 lines (46 loc) · 1021 Bytes
/
Anagram.java
File metadata and controls
61 lines (46 loc) · 1021 Bytes
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.sai;
import java.util.HashMap;
public class Anagram {
private static boolean AnagramCal(String a, String b) {
String str1 = a.replaceAll("\\s", "");
String str2 = b.replaceAll("\\s", "");
if (str1.length() != str2.length()) {
return false;
}
char[] c1 = str1.toCharArray();
char[] c2 = str2.toCharArray();
HashMap<Character, Integer> h1 = new HashMap<Character, Integer>();
for (char c : c1) {
int count = 1;
if (h1.containsKey(c)) {
h1.put(c, h1.get(c) + 1);
} else {
h1.put(c, 1);
}
}
for (char c : c2) {
int count = 0;
if (h1.containsKey(c)) {
h1.put(c, h1.get(c) - 1);
} else {
h1.put(c, 1);
}
}
for (char c : h1.keySet()) {
if (h1.get(c) != 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
String a = "mary";
String b = "army";
boolean x = AnagramCal(a, b);
if (x == true) {
System.out.println("Anagram");
} else {
System.out.println("Not anagram");
}
}
}