forked from ShreyaDhir/HacktoberFest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Anagrams.java
137 lines (102 loc) · 3.24 KB
/
Anagrams.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import java.io.*;
import java.lang.reflect.Array;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.Arrays;
import java.util.concurrent.*;
import java.util.regex.*;
public class Anagrams {
// Complete the sherlockAndAnagrams function below.
static int sherlockAndAnagrams(String s) {
int ctr =0;
char [] substr;
char [] str= s.toCharArray();
int l1 = s.length();
Hashtable<String, Integer> ht = new Hashtable<String, Integer>();
for(int i =1; i<l1; i++)
{
for(int j =0;j<l1-i+1; j++)
{
String val = "";
int a = i+j-1;
for(int k =j; k<=a; k++)
{ val += str[k]; }
if(val.length()>1)
{
substr = val.toCharArray();
Arrays.sort(substr);
val = new String(substr);
}
Boolean value = ht.containsKey(val);
int newVal = (value == false) ? 1 : (ht.get(val) + 1);
ht.put(val, newVal);
}
}
Enumeration e = ht.elements();
while (e.hasMoreElements())
{
int n =(int) e.nextElement();
if(n>1) {ctr += n * (n - 1) / 2; }
}
return ctr;
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
String s = scanner.nextLine();
scanner.close();
int result = sherlockAndAnagrams(s);
}
}
// System.out.println("Length of string "+l1);
// for(int i=0; i<l1-1; i++){
// for(int j = i+1; j<l1; j++){
//
// System.out.println("Value of i "+i);
// System.out.println("Value of j "+j);
//
// if(str[i]==str[j]) {
// System.out.println("Strings are equal");
// //Check for palindrome
// if (j - i == 1) {
// ctr += 1;
// System.out.println("Value of ctr " + ctr);
// } else{
// String checkstring = (s.substring(i, j + 1));
//
// checkstr = checkstring.toCharArray();
//
// //Checking for palindrome
// int l2 = checkstr.length;
// for (int u = 0, v = l2 - 1; u < l2 / 2 && v >= l2 / 2; u++, v--) {
// System.out.println("Value of u " + u);
// System.out.println("Value of v " + v);
//
// if (checkstr[u] != checkstr[v]) {
// System.out.println("Value at u " + checkstr[u]);
// System.out.println("Value at v " + checkstr[v]);
// flag = false;
// }
//
// }
// //Palindrome checking ends
//
// System.out.println("Value of flag " + flag);
// if (flag) {
// ctr += l2 - 1;
// System.out.println("Value of ctr " + ctr);
// } else {
// ctr += 2;
// System.out.println("Value of ctr " + ctr);
// }
// }
// }
// }
// }
//
// //Match for substring
// System.out.println(ctr);
//
//