leetcode || 49、Anagrams

problem:

Given an array of strings, return all groups of strings that are anagrams.

Note: All inputs will be in lower-case.

Hide Tags

Hash Table String

题意:给定多余两组的字符串,找出其中所有的满足以下条件的字符串:(1)字符串的字符种类和对应的数量一样(2)各个字符的位置不作区分

thinking:

(1)很直接想到用hash table来解决这类问题

(2)也可以借用STL map,更加便捷,主键key 存放排序好的string,value 存放指向该string的指针

map<string, vector<const string *> > mapAnagram;

code:

class Solution
{

public:
vector<string> anagrams(vector<string> &strs)
{
    vector<string> ret;
    map<string, vector<const string *> > mapAnagram;

    for (vector<string>::const_iterator it = strs.begin();
        it != strs.end();
        ++it)
    {
        string key(*it);

        sort(key.begin(), key.end());
        mapAnagram[key].push_back(&*it);
    }

    for (map<string, vector<const string *> >::const_iterator it = mapAnagram.begin();
        it != mapAnagram.end();
        ++it)
    {
        if (it->second.size() > 1)
        {
            for (vector<const string *>::const_iterator itstr = it->second.begin();
                itstr != it->second.end();
                ++itstr)
            {
                ret.push_back(**itstr);
            }
        }
    }

    return ret;
}
};
时间: 2024-10-31 22:24:12

leetcode || 49、Anagrams的相关文章

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)Anagrams

Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 题目: 给一组字符串,返回所有满足Anagrams(回文构词法)的字符串: Anagrams是指由颠倒字母顺序组成的单词,比如“dormitory”颠倒字母顺序会变成“dirty room”,“tea”会变成“eat”. 回文构词法有一个特点:单词里的字母的种类和数目没

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变位词归类

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","

[leetcode]49. Group Anagrams重排列字符串分组

是之前的重排列字符串的延伸,判断是重排列后存到HashMap中进行分组 这种HashMap进行分组的方式很常用 public List<List<String>> groupAnagrams(String[] strs) { /* 其实重排列的那个题是有两种做法的,之前整理的是比较好的的一种字母哈希表的方法,还有一种方法 是把字符串排序,然后进行比较就行. 这个题正好就是用了排序的方法,排序后的字符串作为key,list作为value就行 一开始想用字母哈希表的方法,结果没做出来

LeetCode 49. 字母异位词分组(Group Anagrams)

49. 字母异位词分组 49. Group Anagrams 题目描述 给定一个字符串数组,将字母异位词组合在一起.字母异位词指字母相同,但排列不同的字符串. 示例: 输入: ["eat", "tea", "tan", "ate", "nat", "bat"], 输出: [ ??["ate","eat","tea"], ??[&

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】Group Anagrams

题目链接:https://leetcode.com/problems/anagrams/ 题目: Given an array of strings, group anagrams together. For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return: [ ["ate", &q