LeetCode[Hash Table]: Anagrams

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

Note: All inputs will be in lower-case.

思路:对每一个单词的所有字母按照字典顺序排序,排序结果作为key,所有具有相同key的单词组合在一起成为一个Anagram group。最后返回所有的Anagram group。

class Solution {
public:
    vector<string> anagrams(vector<string> &strs) {
        vector<string> result;

        unordered_map<string, vector<string>> dict;
        for (auto word : strs)
            dict[getLetters(word)].push_back(word);

        for (auto wordGroup : dict)
            if (wordGroup.second.size() > 1)
                result.insert(result.end(), wordGroup.second.begin(), wordGroup.second.end());

        return result;
    }

private:
    string getLetters(string word) {
        string letters;
        for    (auto letter : word) {
            int i;
            for (i = 0; i < letters.size(); ++i)
                if (letters[i] > letter)
                    break;

            letters.insert(letters.begin() + i, letter);
        }

        return letters;
    }
};

上面的解法中,对每一个单词按照字典顺序排序采用自己写的插入排序算法,也可以采用<algorthm>中的sort函数。

class Solution {
public:
    vector<string> anagrams(vector<string> &strs) {
        vector<string> result;

        unordered_map<string, vector<string>> dict;
        for (auto word : strs){
            string tmp = word;
            sort(tmp.begin(), tmp.end());
            dict[tmp].push_back(word);
        }

        for (auto group : dict)
            if (group.second.size() > 1)
                result.insert(result.end(), group.second.begin(), group.second.end());

        return result;
    }
};
时间: 2024-10-20 08:51:54

LeetCode[Hash Table]: Anagrams的相关文章

LeetCode[Hash Table]: Two Sum

Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that

LeetCode[Hash Table]: Minimum Window Substring

Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). For example, S = "ADOBECODEBANC" T = "ABC" Minimum window is "BANC". Note: If there is no such windo

LeetCode[Hash Table]: Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note: A valid Sudoku board (partially

[leetcode单元总结]hash table部分easy篇小总结

在difficulty为easy的hash table部分,没有用到常见的哈希算法,更多的是借用数组的下标来实现.对于下标的操作看起来很简单,其实需要细致和耐心,可能一个小错误,比如下标字母弄错用成了上个循环使用的下标(t.t'),结束条件没写对等等就会导致错误. A.在Valid Sudoku 中,判断中拓宽了思维,1.多动脑子,小九宫格中,将每个小个子的下标与第几个联系起来.2.对于一般的含有i,j的双重for循环,比如: for(i=0;i<9;i++) { for(j=0;j<9;j+

[LeetCode] 1. Two Sum_Easy tag: Hash Table

Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Example: Given nums = [2, 7, 11, 15]

【string】hash table, two pointers, string

利用hash table, two pointers, string的题目. 1.求最长不重复子串的长度 hash table体现在一个数组,下标是字符串中元素的ASCII值,下标对应的元素代表该元素在字符串中出现的位置. two pointers体现在用i一步步向前移去遍历字符串中的元素,作为不重复子串的末尾位置:用j指向不重复字符区间的首字符的位置. 1 /*************************** 2 @date 4.23 3 @description https://leet

C 语言构造hash table 解 LC majority element问题

Leetcode上 majority element这题是 有 时间O(N), 空间O(1)的解的. https://leetcode.com/problems/majority-element/ 用hash table来解则为 时间O(N), 空间O(N). 如果是Java里 用HashMap很方便了. 有位同学问怎么用c语言来构造hash table. 我就随手写了一个: typedef struct Node { int val, count; } Node; typedef struct

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来解决

LeetCode781. Rabbits in Forest (Hash Table + Math)

好久没刷题了,今天碰巧看见一道很有趣的题,给大家分享分享: https://leetcode.com/problems/rabbits-in-forest/description/ In a forest, each rabbit has some color. Some subset of rabbits (possibly all of them) tell you how many other rabbits have the same color as them. Those answe