-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnagramHackerRank.java
49 lines (42 loc) · 1.51 KB
/
AnagramHackerRank.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
47
48
import java.util.Scanner;
public class AnagramHackerRank {
static boolean isAnagram(String a, String b) {
// Complete the functio
String A= a.toUpperCase();
String B= b.toUpperCase();
char firststr[] =new char[256];
char secondstr[] =new char[256];
for(char i:A.toCharArray()){
int index=(int)i;
firststr[index]++;
}
for(char i:B.toCharArray()){
int index=(int)i;
secondstr[index]++;
}
for(int i=0;i<256;i++){
if(firststr[i]!=secondstr[i]){
return false;
}
}
return true;
}
// method-02
// char aa[] = a.toLowerCase().toCharArray();
// char bb[] = b.toLowerCase().toCharArray();
// if(a.length() != b.length()){
// return false;
// }else{
// java.util.Arrays.sort(aa);
// java.util.Arrays.sort(bb);
// return java.util.Arrays.equals(aa, bb);
// }
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" );
}
}