-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindAngrams.java
More file actions
66 lines (57 loc) · 2.15 KB
/
Copy pathFindAngrams.java
File metadata and controls
66 lines (57 loc) · 2.15 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
import java.util.*;
//Using HashMap with O(NM) Solution
public class FindAnagrams {
//"cat", "dog", "tac", "god", "act"
private static void printAnagrams(String arr[]) {
// key will be sorted word and values will be list of word having same characters
HashMap<String, List<String> > map = new HashMap<>();
// loop over all words
for (int i = 0; i < arr.length; i++) {//arrlength=5
// convert to char array, sort and
// then re-convert to string
String word = arr[i]; //1.word="cat" 2."dog"
char[] letters = word.toCharArray();//['c','a','t']
Arrays.sort(letters);//['a','c','t']
String newWord = new String(letters);//1.newWord=act 2.newWord=dgo
// calculate hashcode of string
// after sorting
if (map.containsKey(newWord)) {//first time it will be false
//3.map contains word "act" so stored "tac"
//4.map contains word "dgo" so stored "god" in list
//5.map contains word "act" so stored "act" in list
// so we have 3 values for key "act" in list ["cat","tac","act"]
// and 2 values for key "dgo" in list ["dog","god"]
map.get(newWord).add(word);
}
else {//ist time only else will execute
// This is the first time we are
// adding a word for a specific
// hashcode
List<String> words = new ArrayList<>();
words.add(word);//added original word at index i
//words.add("cat")
//words.add("dog")
map.put(newWord, words);
//1.put sorted word "act" as key and value as "cat"
//2.put sorted word "dgo" as key and value as "dog"
//similarly
}
}
// print all the values where size is > 1
// If you want to print non-anagrams,
// just print the values having size = 1
// there is only 2 key value pair in map {"act": "cat","tac","act"} and //{"dgo" : "dog","god"}
for (String s : map.keySet()) {
List<String> values = map.get(s);
if (values.size() > 1) {
System.out.print(values);
}
}
}
public static void main(String[] args)
{
// Driver program
String arr[] = { "cat", "dog", "tac", "god", "act" };
printAnagrams(arr);
}
}