dev-resources.site
for different kinds of informations.
Group Anagrams
Published at
4/17/2022
Categories
leetcode
blind75
strings
Author
Stylus07
Main Article
Given an array of strings strs, group the anagrams together. You can return the answer in any order.
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.
Input: strs = ["eat","tea","tan","ate","nat","bat"]
Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
var groupAnagrams = function (strs) {
if (!strs || !strs.length) {
return null;
}
let anagrams = {};
for (let x = 0; x < strs.length; x++) {
const sortedWord = strs[x].split('').sort().join();
if (sortedWord in anagrams) {
anagrams[sortedWord].push(strs[x])
} else {
anagrams[sortedWord] = [strs[x]];
}
}
return Object.values(anagrams);
}
Time Complexity : O(w*n*log(n))
Space Complexity : O(wn)
Articles
10 articles in total
Container With Most Water
read article
Logger Rate Limiter
read article
Longest Consecutive Sequence
read article
Encode and Decode Strings
read article
Group Anagrams
currently reading
Valid Anagram
read article
Contains Duplicate
read article
Peak Index in a Mountain Array
read article
Intersection of Three Sorted Arrays
read article
Change the document title on react application
read article
Featured ones: