-
Notifications
You must be signed in to change notification settings - Fork 22
/
AnagramGroups
237 lines (204 loc) · 6.73 KB
/
AnagramGroups
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
Question: Given a list of strings, return a list of lists of strings that groups all anagrams.
Ex. given {trees, bike, cars, steer, arcs}
return { {cars, arcs}, {bike}, {trees, steer} }
m = # of words
n = length of longest word
Source: http://www.careercup.com/question?id=5723872416497664
NOTE:
1. PLEASE READ THROUGH ALL THE "ANSWERS AND THEIR RESPECTIVE COMMENTS" TO UNDERSTAND THE REASON WHY MULTIPLE ANSWERS
TO THIS QUESTION IS GIVEN
2. PLEASE UNDERSTAND THAT:
TIME COMPLEXITY(SOLUTION USING HASHMAP) < TIME COMPLEXITY(SOLUTION NOT USING HASHMAP)
3. KEEP IN mind "ALL" the four solutions. LEARN "ALL THE HASH CALCULATION TECHNIQUES ON STRING" (each char value, RLE, XOR)
*******************Answer I) O(m*lgm*n*lgn) Time Complexity, without using HashMap (Using simple sorting)*******************
package anagramGroups;
import java.util.Arrays;
import java.util.Scanner;
public class anagramGroups {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
try{
System.out.println("Enter the number of elements you want to enter in the string array");
int n = in.nextInt();
System.out.println("Enter the elements of the string array");
String[] sArray = new String[n];
for(int i=0;i<n;i++){
sArray[i] = in.next();
sArray[i] = sortString(sArray[i]);
}
Arrays.sort(sArray); // sort the string array. O(mlgm) time complexity.
printArray(sArray);
int count=0;
for(int i=0;i<n-1;i++){
if(sArray[i].equals(sArray[i+1])){
System.out.println("String at Index: "+i+" pairs with string at Index: "+(i+1));
count++;
}
}
if(count==0)
System.out.println("No anagram match found");
}
finally{
in.close();
}
}
private static void printArray(String[] sArray) {
System.out.println("Array elements are:");
for(int i=0;i<sArray.length;i++){
System.out.println(sArray[i].toString());
}
}
public static String sortString(String s){
char[] array = s.toCharArray();
Arrays.sort(array); // sort the characters of the array. O(nlgn) time complexity.
return String.valueOf(array); // convert char array to String
}
}
/*Analysis:
Time Complexity: O(m*lgm*n*logn), where m = No of Strings in String Array
n = Length of each string
Space Complexity: O(m*n)
*/
*******************Answer II) O(m*n) Time Complexity using Multiplicative Hash Function*******************
package anagramGroups;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
public class SolutionUsingHashMap {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
try{
System.out.println("Enter the number of elements in the array");
int n = in.nextInt();
String[] strArray = new String[n];
for(int i=0;i<n;i++)
strArray[i] = in.next();
HashMap<Long,ArrayList<String>> map = new HashMap<Long,ArrayList<String>>();
for(String s : strArray){
long hash = calculateHash(s);
if(!map.containsKey(hash))
map.put(hash, new ArrayList<String>());
map.get(hash).add(s);
}
printHashMap(map);
}
finally{
in.close();
}
}
public static void printHashMap(HashMap<Long, ArrayList<String>> map) {
for(ArrayList<String> s: map.values()){ // VERY IMP: values() method of HashMap returns a Collection
//of values in the HashMap
System.out.println(s); // print the object of ArrayList by calling the toString method
}
}
public static long calculateHash(String s){
long hash=1;
for(int i=0;i<s.length();i++)
hash*=s.charAt(i);
return hash;
}
}
/*Analysis:
Time Complexity: O(m*n), where m = No of Strings in String Array
n = Length of each string
Space Complexity: O(m*n)
*/
********************Answer III) O(m*n) Time Complexity using Intelligent(Run Length String Encoding) Hash Function***********
package anagramGroups;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
public class SolutionUsingIntelligentHash {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
try{
System.out.println("Enter the number of elements in the array");
int n = in.nextInt();
String[] strArray = new String[n];
for(int i=0;i<n;i++)
strArray[i] = in.next();
HashMap<String,ArrayList<String>> map = new HashMap<String,ArrayList<String>>();
for(String s : strArray){
String hash = intelligentHashFunction(s);
if(!map.containsKey(hash))
map.put(hash, new ArrayList<String>());
map.get(hash).add(s);
}
printHashMap(map);
}
finally{
in.close();
}
}
// Run Length Encoding HashFunction
public static String intelligentHashFunction(String s){
int[] charArray = new int[52];
for(int i=0;i<s.length();i++){
charArray[s.charAt(i)-'A']++;
}
StringBuilder str = new StringBuilder();
for(int i=0;i<charArray.length;i++){
if(charArray[i]!=0){
str.append((char)(i+'A')).append(charArray[i]);
}
}
return str.toString();
}
public static void printHashMap(HashMap<String, ArrayList<String>> map) {
for(ArrayList<String> s: map.values()){ // VERY IMP: values() method of HashMap returns a Collection
//of values in the HashMap
System.out.println(s); // print the object of ArrayList by calling the toString method
}
}
}
/*Analysis:
Time Complexity: O(m*n), where m = No of Strings in String Array
n = Length of each string
Space Complexity: O(m*n)
*/
*****Answer IV) O(m*n) Time Complexity using XORHash(Hash calculated using XOR on each character of the String) Hash Function**
package anagramGroups;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
public class SolutionUsingXORHash {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
try{
System.out.println("Enter the number of elements in the array");
int n = in.nextInt();
String[] strArray = new String[n];
for(int i=0;i<n;i++)
strArray[i] = in.next();
HashMap<Integer,ArrayList<String>> map = new HashMap<Integer,ArrayList<String>>();
for(String s : strArray){
int hash = calculateXORHash(s);
if(!map.containsKey(hash))
map.put(hash, new ArrayList<String>());
map.get(hash).add(s);
}
printHashMap(map);
}
finally{
in.close();
}
}
public static void printHashMap(HashMap<Integer, ArrayList<String>> map) {
for(ArrayList<String> s: map.values()){ // VERY IMP: values() method of HashMap returns a Collection of
//values in the HashMap
System.out.println(s); // print the object of ArrayList by calling the toString method
}
}
public static int calculateXORHash(String s){
int xorValue = s.charAt(0);
for(int i=1;i<s.length();i++)
xorValue ^=s.charAt(i);
return xorValue;
}
}
/*Analysis:
Time Complexity: O(m*n), where m = No of Strings in String Array
n = Length of each string
Space Complexity: O(m*n)
*/