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. 关于SpringCloud微服务架构概念的一点理解

    目前微服务是非常火的架构或者说概念,也是在构建大型互联网项目时采用的架构方式. 1.单体架构单体架构,是指将开发好的项目打成war包,然后发布到tomcat等容器中的应用. 假设你正准备开发一款与Ub ...

  2. 3D屏保程序:汉诺塔

    学过程序的人一定记得汉诺塔.我们学的第一个程序是HelloWorld,而碰到的第一个坑就是汉诺塔,短短十几行代码,不知花费了多少时间精力去理解.我记得当年的开发环境还是蓝屏的,汉诺塔程序的输出还是一行 ...

  3. go语言进阶之为结构体类型添加方法

    1.为结构体类型添加方法 示例: package main import "fmt" type Person struct { name string //名字 sex byte ...

  4. 1-2Html与CSS的关系

    Html和CSS的关系 基于微信的后台开发须要掌握的基础知识包括有:HTML.CSS.JavaScript语言. 以下我们就来了解下这三门技术都是用来实现什么的: 1. HTML是网页内容的载体.内容 ...

  5. Swift高速入门之函数

    函数 看一个函数的样例: func addNumbers( let a:Int,let b:Int)->Int{ return a+b; } 实现两个数相加.函数必须以func开头,后面是函数名 ...

  6. UVa 10642 - Can You Solve It?

    题目:二维平面上的整数点.用路径链接起来(0,0)->(1.0)->(0.1)->(2,0)->.. 给你两点坐标.求两点间步长(在路径上的距离). 分析:简单题. 我们发现点 ...

  7. ORA-01109:数据库未打开(解决)

    SQL> startup mountORA-01081: 无法启动已在运行的 ORACLE - 请首先关闭它SQL> shutdown immediateORA-01109: 数据库未打开 ...

  8. curl命令(测试连接命令)

    curl命令是一个利用URL规则在命令行下工作的文件传输工具.它支持文件的上传和下载,所以是综合传输工具,但按传统,习惯称curl为下载工具.作为一款强力工具,curl支持包括HTTP.HTTPS.f ...

  9. 〖Linux〗iptables使用实例

    1. 使局域网用户可共享外网(拨号上网) > /proc/sys/net/ipv4/ip_forward iptables -t nat -A POSTROUTING -o ppp0 -j MA ...

  10. 修改模块搜索路径(注意:py运行完毕,就会失效,他只是临时产生效果)

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #修改模块搜索路径(注意:py运行完毕,就会失效,他只是临时产生效果) import sys print sy ...