Anagrams -- leetcode

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

Note: All inputs will be in lower-case.

算法思路:

将单词进行排序。

用map统计排序后相等的出现次数。

将次数大于1的单词放入结果集。

第1次出现时,因次数最终是否大于1不明郎,将其暂存入另一个数组。

该算法在leetcode上实际执行时间为67ms。

class Solution {
public:
    vector<string> anagrams(vector<string> &strs) {
        vector<string> ans;
        unordered_map<string, int> count;
        vector<pair<string, int> > first;

        for (int i=0; i<strs.size(); i++) {
                string tmp = strs[i];
                sort(tmp.begin(), tmp.end());
                if (count[tmp]++)
                        ans.push_back(strs[i]);
                else
                        first.push_back(make_pair(tmp, i));
        }

        for (auto i: first) {
                if (count[i.first] > 1)
                        ans.push_back(strs[i.second]);
        }

        return ans;
    }
};
时间: 2024-11-06 01:30:45

Anagrams -- leetcode的相关文章

Anagrams leetcode java

题目: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 题解: 这道题看所给的字符串数组里面有多少个是同一个变形词变的.这道题同样使用HashMap来帮助存老值和新值,以及帮忙判断是否是变形词. 首先对每个string转换成char array然后排下序,HashMap里面的key存sort后的词,value存原始的

Group Anagrams Leetcode

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

Solution to LeetCode Problem Set

Here is my collection of solutions to leetcode problems. LeetCode - Course Schedule LeetCode - Reverse Linked List LeetCode - Isomorphic Strings LeetCode - Count Primes LeetCode - Remove Linked List Elements LeetCode - Happy Number LeetCode - Bitwise

No.49 Anagrams[易位构词]

No.49 Anagrams Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. Tags: Hash Table String 难点: 1.没有读清题意,对易位构词的理解不到位 2.题意不明了.输入为一系列字符串,别与回文记混淆了 3.第一次见到,不是很了解,完全没思路 具体分析: 易位构词:两个单词所包含的字符和数量

[LeetCode] Find All Anagrams in a String 找出字符串中所有的变位词

Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100. The order of output does not matter.

[LeetCode]题解(python):049-Groups 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", "

[leetcode]Anagrams @ Python

原题地址:https://oj.leetcode.com/problems/anagrams/ 题意: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 解题思路:anagram的意思是:abc,bac,acb就是anagram.即同一段字符串的字母的不同排序.将这些都找出来.这里使用了哈希表,即Python中的dic

LeetCode: Anagrams [048]

Background Some concepts in Mathematics and Computer Science are simple in one or two dimensions but become more complex when extended to arbitrary dimensions. Consider solving differential equations in several dimensions and analyzing the topology o

[LeetCode]题解(python):049-Group Anagrams

题目来源: https://leetcode.com/problems/anagrams/ 题意分析: 给定一个字符串数组,将用相同字母(包括个数)组成的字符串放到一起.比如["eat", "tea", "tan", "ate", "nat", "bat"],返回 [ ["ate", "eat","tea"], ["n