-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12_Anagrams.java
46 lines (35 loc) · 1.15 KB
/
12_Anagrams.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
44
45
46
import java.util.Scanner;
public class Solution {
static int NO_OF_CHARS = 256;
static boolean isAnagram(String a, String b) {
String alower = a.toLowerCase();
String blower = b.toLowerCase();
char[] str1 = alower.toCharArray();
char[] str2 = blower.toCharArray();
int count1[] = new int[NO_OF_CHARS];
int count2[] = new int[NO_OF_CHARS];
for(int c=0; c<NO_OF_CHARS; c++){
count1[c] = 0;
count2[c] = 0;
}
int i;
for (i = 0; i < str1.length && i < str2.length; i++) {
count1[str1[i]]++;
count2[str2[i]]++;
}
if (str1.length != str2.length)
return false;
for (i = 0; i < NO_OF_CHARS; i++)
if (count1[i] != count2[i])
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" );
}
}