49. Group Anagrams (string, map)

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:

  1. For the return value, each inner list‘s elements must follow the lexicographic order.
  2. All inputs will be in lower-case.

思路:

anagram: 由颠倒字母而成的单词

anagram无关乎字母出现的顺序,所以将字母排序后再比较每个字母出现的次数

class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        if (strs.size() <= 0) return result;

        vector<vector<string>> result;
        map<string,int> anagram;
        int resIndex = 0;
        string s;

        for (int i = 0; i < strs.size(); ++i)
        {
            s = strs[i];
            sort(s.begin(), s.end()); //anagram无关乎字母顺序,所以将string按字母排序后再比较
            if (anagram.find(s) == anagram.end()) { //如果还没有出现过该anagram
                vector<string> item;
                item.push_back(strs[i]);
                result.push_back(item);
                anagram.insert(make_pair(s, resIndex++));
            } else {
                result[anagram[s]].push_back(strs[i]);
            }
        }

        //each inner list‘s elements must follow the lexicographic order
        for(int i = 0; i < result.size(); i++){
            sort(result[i].begin(), result[i].end());
        }
        return result;
    }
};
时间: 2024-12-30 21:10:32

49. Group Anagrams (string, map)的相关文章

49. Group Anagrams(js)

49. Group Anagrams Given an array of strings, group anagrams together. Example: Input: ["eat", "tea", "tan", "ate", "nat", "bat"], Output: [ ["ate","eat","tea"], [&q

LeetCode开心刷题二十六天——49.Group Anagrams

49. Group Anagrams Medium 1824116FavoriteShare Given an array of strings, group anagrams together. Example: Input: ["eat", "tea", "tan", "ate", "nat", "bat"], Output: [ ["ate","eat

Leetcode 49. Group Anagrams

Given an array of strings, group anagrams together. For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return: [ ["ate", "eat","tea"], ["nat",

【一天一道LeetCode】#49. Group Anagrams

一天一道LeetCode系列 (一)题目 Given an array of strings, group anagrams together. For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return: [ ["ate", "eat","tea"

[leetcode]49. Group Anagrams变位词归类

Given an array of strings, group anagrams together. Example: Input: ["eat", "tea", "tan", "ate", "nat", "bat"], Output: [ ["ate","eat","tea"], ["nat","

【LeetCode】49. Group Anagrams

Given an array of strings, group anagrams together. For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return: [ ["ate", "eat","tea"], ["nat",

49. Group Anagrams

Given an array of strings, group anagrams together. For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return: [ ["ate", "eat","tea"], ["nat",

19.2.7 [LeetCode 49] Group Anagrams

Given an array of strings, group anagrams together. Example: Input: ["eat", "tea", "tan", "ate", "nat", "bat"], Output: [ ["ate","eat","tea"], ["nat","

LeetCode 49. Group Anagrams HashTable

Given an array of strings, group anagrams together. Example: Input: ["eat", "tea", "tan", "ate", "nat", "bat"], Output: [ ["ate","eat","tea"], ["nat","