-
Notifications
You must be signed in to change notification settings - Fork 80
/
SherlockAndAnagrams.java
47 lines (47 loc) · 1.19 KB
/
SherlockAndAnagrams.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
import java.io.*;
import java.util.*;
public class Solution
{
public static void main(String[] args)
{
int m,n;
String s="",temp="";
Scanner in = new Scanner(System.in);
m = in.nextInt();
while(m!=0)
{
s = in.next();
n = s.length();
int p = 0,t=0;
int z = (n*n)-(((n*n)-n)/2);
String posi[] = new String[z];
for(int x=0;x<n;x++)
{
for(int y=0;y<n;y++)
{
if(x<=y)
{
temp = s.substring(x,y+1);
char c[] = temp.toCharArray();
Arrays.sort(c);
temp = new String(c);
posi[p] = temp;
p++;
}
}
}
for(int i=0;i<z;i++)
{
for(int j=i+1;j<z;j++)
{
if(posi[i].equals(posi[j]))
{
t++;
}
}
}
System.out.println(t);
m--;
}
}
}