49. Group Anagrams

    xiaoxiao2025-07-12  3

    Given an array of strings, group anagrams together.

    For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return:

    [ ["ate", "eat","tea"], ["nat","tan"], ["bat"] ]

    Note: All inputs will be in lower-case.

    Subscribe to see which companies asked this question

    代码:

    class Solution { public: vector<vector<string>> groupAnagrams(vector<string>& strs) { vector<vector<string>> ans; if(strs.empty()) return ans; map<string,vector<string>> m1; for(auto p:strs) { string temp=p; sort(temp.begin(),temp.end()); m1[temp].push_back(p); } for(auto p:m1) { ans.push_back(p.second) ; } return ans; } };

    转载请注明原文地址: https://ju.6miu.com/read-1300611.html
    最新回复(0)