Anagrams

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

Note: All inputs will be in lower-case.

首先解释一下什么是anagrams:在不考虑顺序的情况下,包含相同字母的字符串组成anagrams

例如:

1、{"eat","ate","tea","coffee"}的anagrams是{"eat","ate","tea"}

2、{"tea","and","ate","eat","dan"}的anagrams是{"tea","ate","eat","and","dan"}

解法一:排序之后的string作为key

在具体实现过程中,构造了两张映射表dict和exist

dict用来对排序过的单词进行快速查找,值(value)为第一个该编码的单词下标。

exist用来记录该排序过的单词是否已存在anagrams中,

如果是,只需要将当前单词装入anagrams;

如果不是,首先要将存放在m中的代表该排序过的单词的第一个单词装入anagrams,再装入当前单词。

class Solution {
public:
vector<string> anagrams(vector<string> &strs) {
vector<string> ret;
unordered_map<string, int> dict;
unordered_map<string, bool> exist; for(int i = ; i < strs.size(); i ++)
{
string temp = strs[i];
sort(temp.begin(), temp.end());
if(dict.find(temp) == dict.end())
dict[temp] = i;
else
{//find anagrams
if(exist[strs[dict[temp]]] == false)
{
ret.push_back(strs[dict[temp]]);
exist[strs[dict[temp]]] = true;
}
ret.push_back(strs[i]);
exist[strs[i]] = true;
}
} return ret;
}
};

解法二:利用质因数分解

背景:任何一个正整数都可以分解为唯一的质因数乘积:n=2a3b5c7d

因此n可以用{a,b,c,d}来唯一表示。

对于这个题,我们对a~z每个单词的出现次数作为每个质因数的次幂,

将乘积进行唯一编码,就可以忽略字母顺序了。相同编码的单词组成anagrams。

因为共有小写字母26个,因此我们需要前26个质因数。

在具体实现过程中,我还构造了两张映射表m和exist

dict用来对编码进行快速查找,值(value)为第一个该编码的单词下标。

exist用来记录该编码是否已存在anagrams中,

如果是,只需要将当前单词装入anagrams;

如果不是,首先要将存放在dict中的代表该编码的第一个单词装入anagrams,再装入当前单词。

class Solution {
public:
vector<string> anagrams(vector<string> &strs) {
vector<string> ret;
vector<long long int> code(strs.size(), );
CalCode(strs, code);
map<long long int, int> dict;
map<string, bool> exist; for(int i = ; i < strs.size(); i ++)
{
if(dict.find(code[i]) == dict.end())
dict[code[i]] = i;
else
{
if(exist[strs[dict[code[i]]]] == false)
{
exist[strs[dict[code[i]]]] = true;
ret.push_back(strs[dict[code[i]]]);
}
exist[strs[i]] = true;
ret.push_back(strs[i]);
}
}
return ret;
}
void CalCode(vector<string> &strs, vector<long long int> &code)
{
for(int i = ; i < strs.size(); i ++)
{
string cur = strs[i];
vector<int> count(, ); //a~z count
for(int j = ; j < cur.size(); j ++)
{
count[cur[j]-'a'] ++;
}
double prime[] = {,,,,,,,,,,,,,,,,,,,,,,,,,};
long long result = ;
for(int j = ; j < ; j ++)
{
result *= (long long int)pow(prime[j], count[j]);
}
code[i] = result;
}
}
};

【LeetCode】49. Anagrams (2 solutions)的更多相关文章

  1. 【LeetCode】49. Group Anagrams 解题报告(Python & Java & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序+hash 日期 题目地址:https://le ...

  2. 【LeetCode】49. Group Anagrams

    题目: Given an array of strings, group anagrams together. For example, given: ["eat", " ...

  3. 【LeetCode】49. 字母异位词分组

    49. 字母异位词分组 知识点:字符串:哈希表 题目描述 给你一个字符串数组,请你将 字母异位词 组合在一起.可以按任意顺序返回结果列表. 字母异位词 是由重新排列源单词的字母得到的一个新单词,所有源 ...

  4. 【LeetCode】18. 4Sum (2 solutions)

    4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d  ...

  5. 【LeetCode】46. Permutations (2 solutions)

    Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...

  6. 【LeetCode】120. Triangle (3 solutions)

    Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...

  7. 【LeetCode】77. Combinations (2 solutions)

    Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ...  ...

  8. 【LeetCode】78. Subsets (2 solutions)

    Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...

  9. 【LEETCODE】49、数组分类,简单级别,题目:566,1089

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

随机推荐

  1. 启动Memcached报错:/usr/local/memcached/bin/memcached: error while loading shared libraries: libevent-2.1.so.6: cannot open shared object file: No such file or directory

    1.查找文件放在哪里 sudo find / -name libevent-2.1.so.6 发现放在/usr/local/lib/libevent-2.1.so.6下. 2.创建软链接 sudo l ...

  2. 【BZOJ】【2738】&【Tsinsen】【A1333】矩阵乘法

    整体二分+树状数组 过了[BZOJ][2527][POI2011]Meteors以后这题就没那么难啦~ 关键是[从小到大]依次插入数字,然后整体二分每个查询的第k大是在第几次插入中被插入的……嗯大概就 ...

  3. 3D屏保:排色榜

    3D屏保:排色榜 排色榜,是一个针对图形学中的色彩进行排序的DEMO,这里的色是色彩的意思,看成别的点进来的同学请自觉面壁.该DEMO可以按RGB,GBR,BRG,灰度值四种方式进行排序.排序算法为冒 ...

  4. Java JDBC数据库链接

    好久没有编写有关数据库应用程序啦,这里回顾一下java JDBC. 1.使用Java JDBC操作数据库一般需要6步: (1)建立JDBC桥接器,加载数据库驱动: (2)连接数据库,获得Connect ...

  5. Loadrunner视频教程汇总

    小布老师视频:测试工具概述,兼LoadRunner介绍 -1-4http://www.boobooke.com/v/bbk1046http://www.boobooke.com/v/bbk1046.z ...

  6. 使用swipemenulistview实现列表的左右滑动

    今天从网上找到一个第三方控件swipemenulistview,封装好的一个控件,可以实现列表的左右滑动,模仿qq的列表效果 下载地址为:https://github.com/baoyongzhang ...

  7. IE与Cognos的那些事

    问题描述1:打开报表设计页面的时候,即打开reportstudio的时候报IE阻止了一个来自XX.XX.XX.XX的弹出窗口程序 IE设置:关闭弹出窗口阻止程序即可 问题描述2:无法下载文件,例如Ex ...

  8. 我为何放弃Gulp与Grunt,转投npm scripts(上)

    本文来源于我在InfoQ中文站翻译的文章.原文地址是:http://www.infoq.com/cn/news/2016/02/gulp-grunt-npm-scripts-part1 Cory Ho ...

  9. 拼接多个 wchar_t *

      /* wcscat example */ #include <wchar.h>   int main () { wchar_t wcs[80]; wcscpy (wcs,L" ...

  10. Angular路由与Nodejs路由的区别

    转自:http://www.imooc.com/qadetail/114683?t=148182 觉得angualr.js的路由是针对于单页面的路由,每次路由发生变化,只是页面的状态发生变化,页面本身 ...