forked from Red-0111/Anything-Repo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_anagram.java
More file actions
66 lines (63 loc) · 1.2 KB
/
check_anagram.java
File metadata and controls
66 lines (63 loc) · 1.2 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.util.*;
import java.io.*;
class Anagram
{
public static void main(String[] args)
{
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number of test case:");
int t =Integer.parseInt(br.readLine());
String s1="",s2="",s="";
String[] ans =new String[t];
for(int i=0;i<t;i++)
{
System.out.println("Enter the "+(i+1)+" string separated by single space:");
s=br.readLine();
int l=s.lastIndexOf(' ');
s1=s.substring(0,l);
s2=s.substring(l+1);
if(anagrams(s1,s2)==1)
{
ans[i]="Yes";
}
else
{
ans[i]="No";
}
}
for(int i1=0;i1<t;i1++)
{
System.out.println(ans[i1]);
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
static int anagrams(String s1,String s2)
{
if(s1.length()!=s2.length())
return 0;
else
{
if(s1.equals(s2))
{
return 1;
}
else
{
char ss1[]=s1.toCharArray();
char ss2[]=s2.toCharArray();
Arrays.sort(ss1);
Arrays.sort(ss2);
if(Arrays.equals(ss1, ss2))
return 1;
else
return 0;
}
}
}
}