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
23 changes: 23 additions & 0 deletions Leetcode Group Anagrams.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string>>ans; // created a ans vector to store our answer

int n=strs.size();
// created a unorderes map to stor the mapping b/w the sorted word and orginal word


unordered_map<string,vector<string>>mp;

for(int i=0;i<n;i++){
string s=strs[i];
sort(strs[i].begin(),strs[i].end()); // sort the word
mp[strs[i]].push_back(s); // do mapping
}
for(auto it:mp){
ans.push_back(it.second); //push it.second
}

return ans; // finally return our ans vector
}
};