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

Create Group_Anagrams.cpp #124

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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;

}
};