diff --git a/LeetCode Problems/Group_Anagrams.cpp b/LeetCode Problems/Group_Anagrams.cpp new file mode 100644 index 0000000..b7c255b --- /dev/null +++ b/LeetCode Problems/Group_Anagrams.cpp @@ -0,0 +1,36 @@ +class Solution { +public: + vector> groupAnagrams(vector& strs) { + + vector>ans; + + unordered_map>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; + + } +};