15
15
public class AnagramsTogether {
16
16
17
17
/**
18
- * Prints all the anagrams together from the string
19
- * array {@param s}.
18
+ * Prints all the anagrams together from the string array {@code strings}.
20
19
* <p/>
21
20
* Anagrams are words consisting of the same letters but in the same or different
22
21
* order. For example, "cat" and "tac" are anagrams. Same as "god" and "dog".
23
22
*
24
- * @param s
23
+ * @param strings
25
24
*/
26
- private static void printAnagramsTogether (String [] s ) {
25
+ private static void printAnagramsTogether (String [] strings ) {
27
26
28
27
// each key holds all the indexes of a anagram
29
28
HashMap <String , List <Integer >> hashMap = new HashMap <>();
30
29
31
- for (int i = 0 ; i < s .length ; i ++) {
32
- char [] chars = s [i ].toCharArray ();
30
+ for (int i = 0 ; i < strings .length ; i ++) {
31
+ char [] chars = strings [i ].toCharArray ();
33
32
Arrays .sort (chars );
34
33
35
34
List <Integer > indexes = hashMap .get (String .valueOf (chars ));
@@ -42,7 +41,7 @@ private static void printAnagramsTogether(String[] s) {
42
41
43
42
for (Map .Entry <String , List <Integer >> entry : hashMap .entrySet ()) {
44
43
for (int i = 0 ; i < entry .getValue ().size (); i ++) {
45
- System .out .println (s [entry .getValue ().get (i )]);
44
+ System .out .println (strings [entry .getValue ().get (i )]);
46
45
}
47
46
System .out .println ("------" );
48
47
}
@@ -52,4 +51,4 @@ public static void main(String[] args) {
52
51
printAnagramsTogether (new String []{"cat" , "dog" , "tac" , "god" , "act" });
53
52
printAnagramsTogether (new String []{"cat" , "tac" , "act" , "god" , "dog" });
54
53
}
55
- }
54
+ }
0 commit comments