forked from riya2001-cloud/java-hacktober
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnagram.java
40 lines (36 loc) · 1.03 KB
/
Anagram.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
import java.util.*;
public class Anagram{
public static boolean checkAnagram(String a, String b){
if(a.length()!=b.length()){
return false;
}
else{
char[] ch1 = a.toLowerCase().toCharArray();
char[] ch2 = b.toLowerCase().toCharArray();
int[] cnt = new int[26];
for(int i=0;i<ch1.length;i++){
cnt[ch1[i]-97]++;
}
for(int i=0;i<ch2.length;i++){
cnt[ch2[i]-97]--;
}
for(int i=0;i<cnt.length;i++){
if(cnt[i]!=0){
return false;
}
}
return true;
}
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String s1 = sc.nextLine();
String s2 = sc.nextLine();
if(checkAnagram(s1, s2)){
System.out.println("Anagrams");
}
else{
System.out.println("Not Anagrams");
}
}
}