-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnagram.java
40 lines (40 loc) · 1.1 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 void main(String args[])
{
Scanner sc=new Scanner(System.in);
String s1=sc.nextLine();
String s2=sc.nextLine();
String t1=s1.toLowerCase();
String t2=s2.toLowerCase();
//form arrays
String a1[]=t1.split(" ");
String a2[]=t2.split(" ");
char c1[]=new char[t1.length()-a1.length+1];
char c2[]=new char[t2.length()-a2.length+1];
int k=0;
for(int i=0;i<a1.length;i++)
{
for(int j=0;j<a1[i].length();j++)
c1[k++]=a1[i].charAt(j);
}
k=0;
for(int i=0;i<a2.length;i++)
{
for(int j=0;j<a2[i].length();j++)
c2[k++]=a2[i].charAt(j);
}
Arrays.sort(c1);
Arrays.sort(c2);
boolean flag=true;
for(int i=0;i<k;i++)
if(c1[i]!=c2[i])
flag=false;
System.out.print("The words \""+s1+"\" and \""+s2+"\"");
if(flag)
System.out.println(" are Anagrams");
else
System.out.println(" are not Anagrams");
}
}