Leetcode 49. Group Anagrams
Shout out to NeetCode for this solution
I am a developer from Nashville, TN. I specialize in the .NET tech stack. I have created many projects in Blazor WASM, Xamarin, MAUI, etc.
Search for a command to run...
Shout out to NeetCode for this solution
I am a developer from Nashville, TN. I specialize in the .NET tech stack. I have created many projects in Blazor WASM, Xamarin, MAUI, etc.
No comments yet. Be the first to comment.
A series documenting my journey to improving my ability to solve LeetCode problems through YouTube videos, study plans, articles, etc. Using my own words for later reference.
Intuition Get the length of the subsequence (if any) and subtract it from the length of t to find out how many characters we have to append (if any) Approach Keep a count of the length of the subsequence while iterating through s and then subtract th...
Intuition Get the length of the subsequence (if any) and subtract it from the length of t to find out how many characters we have to append (if any) Approach Keep a count of the length of the subsequence while iterating through s and then subtract th...
Intuition Sort the array and check if the start and end of the array match. If not, increment or decrement accordingly. Approach First sort the array. Then use two pointers. One at the beginning, the other at the end. Check to see if the value at the...
Intuition Find position of ch (if any) then use two pointers to swap characters until string is reveresed from beginning to index of ch Approach Start by checking each character for ch. If we find it, that's where our right pointer will start. Our le...
Intuition Use two pointers to iterate through the string checking each adjacent character to see if it is "bad" or "good". Approach First if the string is 1 or fewer characters we know it is good so we can just return it. If that's not the case, we c...
Intuition Since both arrays are sorted, we can find the minimum common value by iterating from left to right with two pointers. Approach Initialize both pointers to 0 to start at the beginning of the array. While both pointers are within bounds of th...
This is going to be my first Leetcode post. As a disclaimer: I am by no means an expert. I am, in fact, just trying to learn Leetcode problems and I think this will be a great tool to help solidify the information, tricks, and patterns I learn from doing them.
I am also hoping that if someone stumbles upon this it will be of some help because, personally, the more ways something is explained to me, the better chance I have of understanding it.
Use a Dictionary to map each word to its list of anagrams
by sorting each word for easier comparison. Where key is the sorted word and the value is a list of its anagrams contained in the strs array.
Loop through each word in strs and at each word:
Sort the word.
Check if the dictionary contains the sorted word as a key which would indicate an anagram of that word does exist, and we want to group this one with it.
Add this word to the list of anagrams which is attached to these sorted letters. Where the sorted letters are the dictionary key and the current anagram is added to the corresponding list which is the value attached to the key.
public class Solution
{
public IList<IList<string>> GroupAnagrams(string[] strs)
{
var anagrams = new Dictionary<string, List<string>>();
IList<IList<string>> ans = new List<IList<string>>();
for(int i = 0; i < strs.Length; i++)
{ //get the current word in strs
var temp = strs[i].ToCharArray();
//sort that word. i.e in the first iteration temp will
//be [a, e, t]
Array.Sort(temp);
//convert the word to a string so we can add it to the
//dictionary
var current = new string(temp);
//if this sorted word is in the dictionary then it is
//an anagram of another word with the same letters
if(anagrams.ContainsKey(current))
{
//add the current word (eat) to the list attached to
//the sorted word 'aet'
anagrams[current].Add(strs[i]);
}
else
{
//if there is not a key for this sorted word either
//there is no matching anagram, or we just haven't
//found it yet. So add it to the dictionary
anagrams.Add(current,
new List<string>{strs[i]});
//add the list of anagrams attached to this sorted
//word to the answer we will be returning
ans.Add(anagrams[current]);
}
}
//return the list of lists of anagrams
return ans;
}
}