-
Notifications
You must be signed in to change notification settings - Fork 0
/
Anagram.java
53 lines (48 loc) · 1.21 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
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.util.Scanner;
public class Anagram {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s1=sc.nextLine();
String s2=sc.nextLine();
sc.close();
String s="abcdefghijklmnopqrstuvwxyz";
String l[]=new String[s.length()];int m[]=new int[s.length()];int n[]=new int[s.length()];
for(int i=0;i<s.length();i++)
{
m[i]=0;
n[i]=0;
}
for(int i=0;i<s.length();i++)
{
l[i]=s.substring(i,i+1);
}
for(int i=0;i<l.length;i++)
{
for(int j=0;j<s1.length();j++)
{
if(l[i].equalsIgnoreCase(s1.substring(j,j+1))){
m[i]++;}
}
}
for(int i=0;i<l.length;i++)
{
for(int j=0;j<s2.length();j++)
{
if(l[i].equalsIgnoreCase(s2.substring(j,j+1))){
n[i]++;}
}
}
int k=0;
for(int i=0;i<s.length();i++)
{
if(m[i]!=n[i]){
System.out.println("Not Anagrams");
break;}
else{
k++;
}
}
if(k==(s.length()) && m[s.length()-1]==n[s.length()-1])
System.out.println("Anagrams");
}
}