-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanagram.java
61 lines (57 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
54
55
56
57
58
59
60
61
import java.io.*;
import java.util.*;
class anagram
{
public static void main(String[] args)throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
String s[] = str.split("\\s+");
int n = s.length;
String[] output = new String[n];
int count = 0;
boolean isfound = false;
boolean match = false;
for(int i =0;i<n;i++)
{
String a = (s[i]).toLowerCase();
char[] ch = a.toCharArray();
Arrays.sort(ch);
for(int j = i+1; j<n;j++)
{
String b = (s[j]);
char[] bh = b.toCharArray();
Arrays.sort(bh);
if(!a.equals(b) && Arrays.equals(ch,bh))
{
for(int k = 0;k<i;k++)
{
char[] ah = s[k].toLowerCase().toCharArray();
Arrays.sort(ah);
if(a.equalsIgnoreCase(s[k]) || Arrays.equals(ah, ch))
match = true;
}
if(!match)
{
isfound = true;
count++;
if(count==1)
System.out.print(a+" ");
boolean smatch = false;
for(int m = 0;m<j;m++)
{
if(b.equalsIgnoreCase(s[m]))
smatch = true;
}
if(!smatch)
System.out.print(b+" ");
}
}
}
if(isfound)
System.out.println();
count = 0;
isfound = false;
}
}
}