LeetCode - 49. Group Anagrams

简介: 49. Group Anagrams  Problem's Link  ---------------------------------------------------------------------------- Mean:  给定一个由string类型构成的集合,让你按照每个字符串的单词构成集合来将这个集合分类.

49. Group Anagrams 

Problem's Link

 ----------------------------------------------------------------------------

Mean: 

给定一个由string类型构成的集合,让你按照每个字符串的单词构成集合来将这个集合分类.

analyse:

STL的运用.

Time complexity: O(N)

 

view code

 

/**
 * -----------------------------------------------------------------
 * Copyright (c) 2016 crazyacking.All rights reserved.
 * -----------------------------------------------------------------
 *       Author: crazyacking
 *       Date  : 2016-03-08-20.40
 */
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long(LL);
typedef unsigned long long(ULL);
const double eps(1e-8);

class Solution
{
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs)
    {
        map<string,vector<string>> mp;
        for(int i=0;i<strs.size();++i)
        {
            string _str=strs[i];
            sort(_str.begin(),_str.end());
            mp[_str].push_back(strs[i]);
        }

        vector<vector<string>> res;
        for(auto mp_ptr:mp)
        {
            vector<string> _res;
            for(auto _mp_ptr:mp_ptr.second)
            {
                _res.push_back(_mp_ptr);
            }
            sort(_res.begin(),_res.end());
            res.push_back(_res);
        }
        return res;
    }
};

int main()
{
    int n;
    while(cin>>n)
    {
        vector<string> strs(n);
        for(int i=0;i<n;++i)
            cin>>strs[i];
        Solution solution;
        auto ans=solution.groupAnagrams(strs);
        for(auto p1:ans)
        {
            for(auto p2:p1)
                cout<<p2<<" ";
            cout<<endl;
        }
    }
    return 0;
}

 

目录
相关文章
LeetCode 438. 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.
85 0
LeetCode 438. Find All Anagrams in a String
|
C语言 Python
LeetCode 49. Group Anagrams
给定一组字符串,将由相同字母组成的字符串组合在一起。 注意:所有给定的输入都是小写,输出的顺序不重要
69 0
|
存储 Java 索引
LeetCode 49: 字母异位词分组 Group Anagrams
# LeetCode 49: 字母异位词分组 Group Anagrams ### 题目: 给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。 Given an array of strings, group anagrams together.
731 0
|
存储 测试技术
(转)leetcode:Find All Anagrams in a String 滑动窗口方法总结
今天做了几道滑动窗口的题,稍微总结一下。 起因源于早上在leetcode上pick one,随机到了一个easy的题目,想着随便做了,结果半天也找不到最优解,耗时300多ms,A是A了,不过就是暴力罢了。
1696 0
|
12天前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
2月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
47 6