Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solved group anagrams problem #16

Merged
merged 1 commit into from
Oct 28, 2022
Merged
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
solved group anagrams problem
  • Loading branch information
im-sumit committed Oct 28, 2022
commit 07a66473912e0aa5b367d32fd41d4b3d7ba746e6
33 changes: 33 additions & 0 deletions solutions/group_anagrams.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// problem link:

#include<bits/stdc++.h>
#include<unordered_map>

using namespace std;

class Solution {
public:
vector<vector<string> > groupAnagrams(vector<string>& words) {
unordered_map<string, vector<string>> umap;
for (string word: words) {
string copy = "";
for (char ch: word) copy.push_back(ch);
sort(copy.begin(), copy.end());
if (umap.find(copy) != umap.end()) {
umap[copy].push_back(word);
} else {
umap[copy] = { word };
}
}
vector<vector<string> > ans;
for(auto p: umap) {
ans.push_back(p.second);
}
return ans;
}
};

int main() {
Solution s;

}