Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions LeetCode Problems/Group_Anagrams.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {

vector<vector<string>>ans;

unordered_map<string, vector<string>>mp;

/*
Consider example 1 : strs = ["eat","tea","tan","ate","nat","bat"]

After the below opeartion of for loop map will contain

aet -- eat, tea, ate
ant -- tan, nat
abt -- bat
*/

for(int i = 0 ; i < strs.size() ; i++)
{
string s = strs[i];
sort(strs[i].begin(),strs[i].end());
mp[strs[i]].push_back(s);
}

//now simply put the elements of second column of map in ans

for(auto i : mp)
{
ans.push_back(i.second);
}

return ans;

}
};