Skip to content

Commit c507286

Browse files
authored
Merge pull request #1586 from akgmage/dev
update group anagrams in java
2 parents 74564ee + 1ae5113 commit c507286

File tree

1 file changed

+64
-39
lines changed

1 file changed

+64
-39
lines changed

Strings/group_anagrams.java

Lines changed: 64 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,80 @@
1-
/*Given an array of strings strs, group the anagrams together. You can return the answer in any order.
1+
/*
2+
Group Anagrams
23
3-
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
4+
Sample Input: = ["yo", "act", "flop", "tac", "foo", "cat", "oy", "olfp"]
5+
Output: [["yo", "oy"], ["flop", "olfp"], ["act", "tac", "cat"], ["foo"]]
46
5-
7+
Explanation:
68
7-
Example 1:
9+
The code snippet is a function that groups anagrams together. An anagram is a word or phrase formed by rearranging
10+
the letters of another word or phrase.
811
9-
Input: strs = ["eat","tea","tan","ate","nat","bat"]
10-
Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
11-
Example 2:
12+
The function first defines two functions: GroupAnagrams and sortWord. The GroupAnagrams function takes a list of words
13+
as input and returns a list of lists, where each inner list contains all the anagrams of a word in the original list.
14+
The sortWord function takes a word as input and returns a string that contains the word's letters in sorted order.
1215
13-
Input: strs = [""]
14-
Output: [[""]]
15-
Example 3:
16+
The GroupAnagrams function works by first creating a map where the keys are sorted strings of words and the values are
17+
lists of words that have the same sorted string. Then, the function iterates through the list of words, calling the
18+
sortWord function to get the sorted string for each word. The function then adds the word to the list of words associated with the sorted string in the map. Finally, the function iterates through the map, adding each list of words to a list of lists.
1619
17-
Input: strs = ["a"]
18-
Output: [["a"]]
19-
*/
20-
// Approach:
21-
// 1. Go through the list of words
22-
// 2. Sort each word. All annograms of the word would look the same being sorted.
23-
// 3. Find matching bucket in map and put the word to it
24-
// 4. When finished, convert the map to vector of vectors and return it
20+
The sortWord function works by converting the word to a byte array and then calling the sort.Slice function to sort
21+
the byte array. The function then returns a string that contains the sorted byte array.
2522
26-
package Strings;
23+
O(w * n * log(n)) time | O(wn) space - where w is the number of words and n is the length of the longest word
2724
28-
import java.util.*;
25+
*/
26+
import java.util.ArrayList;
27+
import java.util.Arrays;
28+
import java.util.HashMap;
29+
import java.util.List;
30+
import java.util.Map;
2931

30-
public class groupAnagram {
32+
public class GroupAnagrams {
33+
public List<List<String>> groupAnagrams(String[] words) {
34+
// Create a map where the keys are sorted strings of words and the values are lists of words
35+
// that have the same sorted string.
36+
Map<String, List<String>> anagrams = new HashMap<>();
3137

32-
public List<List<String>> groupAnagrams(String[] strs) {
38+
// Iterate through the words
39+
for (String word : words) {
40+
// Get the sorted string for the word
41+
String sortedWord = sortWord(word);
3342

34-
HashMap<String , List<String>> map = new HashMap<>();
35-
36-
for(int i = 0 ; i < strs.length ; i++) {
37-
char[] charArr = strs[i].toCharArray();
38-
Arrays.sort(charArr);
39-
String string = new String(charArr);
40-
41-
if(!map.containsKey(string)){
42-
map.put(string, new ArrayList<>());
43+
// Add the word to the list of words associated with the sorted string in the map
44+
if (!anagrams.containsKey(sortedWord)) {
45+
anagrams.put(sortedWord, new ArrayList<>());
4346
}
44-
map.get(string).add(strs[i]);
45-
}
46-
47-
List<List<String>> result = new ArrayList<>();
48-
49-
for(String st : map.keySet()) {
50-
List<String> list = map.get(st);
51-
result.add(list);
47+
anagrams.get(sortedWord).add(word);
5248
}
49+
50+
// Create a list of lists, where each inner list contains all the anagrams of a word in the original array
51+
List<List<String>> result = new ArrayList<>(anagrams.values());
52+
53+
// Return the list of lists
5354
return result;
5455
}
56+
57+
// Helper method to sort the characters in a word and return the sorted string
58+
private String sortWord(String word) {
59+
// Convert the word to a character array
60+
char[] wordChars = word.toCharArray();
61+
62+
// Sort the character array
63+
Arrays.sort(wordChars);
64+
65+
// Return the sorted string
66+
return new String(wordChars);
67+
}
68+
69+
public static void main(String[] args) {
70+
// Example usage
71+
String[] words = {"eat", "tea", "tan", "ate", "nat", "bat"};
72+
GroupAnagrams groupAnagrams = new GroupAnagrams();
73+
List<List<String>> groupedAnagrams = groupAnagrams.groupAnagrams(words);
74+
75+
// Print the grouped anagrams
76+
for (List<String> group : groupedAnagrams) {
77+
System.out.println(group);
78+
}
79+
}
5580
}

0 commit comments

Comments
 (0)