Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- typedef pair<string, int> II;
- vector<vector<string>> groupAnagrams(vector<string>& strs) {
- vector<II> temp;
- for (int i = 0; i < strs.size(); i++) {
- string x = strs[i];
- sort(x.begin(), x.end());
- temp.push_back({x, i});
- }
- sort(temp.begin(), temp.end());
- vector<vector<string>> ans;
- vector<string> cur;
- for (int i = 0; i < temp.size(); i++) {
- if (i == 0 || temp[i].first == temp[i - 1].first)
- cur.push_back(strs[temp[i].second]);
- else {
- ans.push_back(cur);
- cur.clear();
- cur.push_back(strs[temp[i].second]);
- }
- }
- ans.push_back(cur);
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement