Skip to content

Commit 74564ee

Browse files
authored
Merge pull request #1585 from akgmage/dev
update group anagrams
2 parents 46eba0b + fbc3c56 commit 74564ee

File tree

1 file changed

+53
-30
lines changed

1 file changed

+53
-30
lines changed

Strings/group_anagrams.js

Lines changed: 53 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,62 @@
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-
Example 1:
6-
Input: strs = ["eat","tea","tan","ate","nat","bat"]
7-
Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
7+
Explanation:
88
9-
Example 2:
10-
Input: strs = [""]
11-
Output: [[""]]
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.
1211
13-
Example 3:
14-
Input: strs = ["a"]
15-
Output: [["a"]]
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.
15+
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.
19+
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.
22+
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
1624
17-
18-
* @param {string[]} strs
19-
* @return {string[][]}
2025
*/
21-
// Approach:
22-
// 1. Go through the list of words
23-
// 2. Sort each word. All annograms of the word would look the same being sorted.
24-
// 3. Find matching bucket in map and put the word to it
25-
// 4. When finished, convert the map to vector of vectors and return it
26-
27-
function groupAnagrams(strs) {
28-
let result = {};
29-
30-
for (let i of strs) {
31-
let anagram = i.split("").sort().join("");
32-
if (result[anagram]) {
33-
result[anagram].push(i);
34-
} else {
35-
result[anagram] = [i];
26+
function sortWord(word) {
27+
// Sort the characters in the word and return the sorted string
28+
return word.split("").sort().join("");
29+
}
30+
31+
function groupAnagrams(words) {
32+
// Create an object where the keys are sorted strings of words and the values are arrays of words
33+
// that have the same sorted string.
34+
const anagrams = {};
35+
36+
// Iterate through the words
37+
for (const word of words) {
38+
// Get the sorted string for the word
39+
const sortedWord = sortWord(word);
40+
41+
// Add the word to the array associated with the sorted string in the object
42+
if (!anagrams[sortedWord]) {
43+
anagrams[sortedWord] = [];
3644
}
45+
anagrams[sortedWord].push(word);
3746
}
38-
return Object.values(result);
47+
48+
// Create an array of arrays, where each inner array contains all the anagrams of a word in the original array
49+
const result = Object.values(anagrams);
50+
51+
// Return the array of arrays
52+
return result;
53+
}
54+
55+
// Example usage
56+
const words = ["eat", "tea", "tan", "ate", "nat", "bat"];
57+
const groupedAnagrams = groupAnagrams(words);
58+
59+
// Print the grouped anagrams
60+
for (const group of groupedAnagrams) {
61+
console.log(group);
3962
}

0 commit comments

Comments
 (0)