Last year, I had a technical interview
and one of the questions was on Anagrams
. I solved the problem in 3 ways today and I want to share it with you in a moment.
Question: Given an array of strings, group anagrams together. Anagram: These are words that are made up of the same letters but in different orders. Example: Input: ["eat", "tea", "tan", "ate", "nat", "bat"] Output: [ ["ate","eat","tea"], ["nat","tan"], ["bat"] ]
If you have been following my Algorithm series, then you are well fortified for this challenge.
groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]);
/*
[ [ 'ate', 'eat', 'tea' ], [ 'nat', 'tan' ], [ 'bat' ] ]
*/
Prerequisite
Solution
- .map(), sort(), join(), Set(), forEach(), filter(), push, spread operator
function groupAnagrams(array) {
let finalArray = [];
// rearrange each word to check for anagram
let rearranged = array.map(element => {
return [...element].sort().join("");
});
// remove duplicates
let uniqueArray = [...new Set(rearranged)];
// compare original array with dupliates
uniqueArray.forEach(word => {
let chunk = array.filter(char => word === [...char].sort().join(""));
finalArray.push(chunk.sort());
});
return finalArray;
}
- for...of...loop, sort(), join(), filter(), push, split(), indexOf()
function groupAnagrams(array) {
let finalArray = [];
let rearranged = [];
// rearrange each word to check for anagram
for (element of array) {
rearranged.push(
element
.split("")
.sort()
.join("")
);
}
// remove duplicates
let uniqueArray = rearranged.filter(
(member, index) => rearranged.indexOf(member) === index
);
// compare original array with dupliates
for (word of uniqueArray) {
let chunk = [];
for (char of array) {
if (
word ===
char
.split("")
.sort()
.join("")
) {
chunk.push(char);
}
}
finalArray.push(chunk.sort());
}
return finalArray;
}
- for...loop, while...loop, sort(), join(), push(), split(), includes()
function groupAnagrams(array) {
let finalArray = [];
let rearranged = [];
// rearrange each word to check for anagram
let i = 0;
while (i < array.length) {
rearranged.push(
array[i]
.split("")
.sort()
.join("")
);
i++;
}
// remove duplicates
let uniqueArray = [];
for (let j = 0; j <= rearranged.length; j++) {
if (!uniqueArray.includes(rearranged[j])) {
uniqueArray.push(rearranged[j]);
}
}
// compare original array with dupliates
let counter = 0;
while (counter < uniqueArray.length) {
let chunk = [];
for (let k = 0; k < array.length; k++) {
if (
uniqueArray[counter] ===
array[k]
.split("")
.sort()
.join("")
) {
chunk.push(array[k]);
}
}
if (chunk.length != 0) {
finalArray.push(chunk.sort());
}
counter++;
}
return finalArray;
}
Conclusion
Interview questions like this one we just solved tends to test how far you have dived into algorithm. As you must have noted, the solution to just this problem is built upon other 5 algorithms we have solved in the past. So starting from the basics is very important.
There are many ways to solve problems programmatically. I will love to know other ways you solved yours in the comment section.
If you have questions, comments or suggestions, please drop them in the comment section.
You can also follow and message me on social media platforms.
Thank You For Your Time.
Top comments (6)
Great article, and good interview question.
Good work, keep it up! :)
Just wanted to share my approach to this:
What do you think about it?
Thank you Wojciech. I love your method. It's very clever and coincise. Works perfectly too.
dev-to-uploads.s3.amazonaws.com/i/...
Do you mind explaining what is going on in the reduce() function probably by commenting the code?
I thought of reducing array of anagrams to an object which keys will be initial words and values arrays of anagrams from these words.
Tried pretty much in the same way
function groupAnagrams(array) {
var reducedObject = array.reduce((acc, cur) => {
var newcur=cur.split('').sort().join('');
if (!acc[newcur]) {
acc[newcur] = [cur]
} else {
acc[newcur] = [...acc[newcur], cur]
}
return acc;
}, {});
return Object.values(reducedObject);
}
console.log(groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]));
Nice! :)
Bruno, Thank you for taking your time to read through!