首页 技术 正文
技术 2022年11月20日
0 收藏 601 点赞 3,250 浏览 2003 个字

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;
}
};

【LeetCode】49. Anagrams (2 solutions)

解法二:利用质因数分解

背景:任何一个正整数都可以分解为唯一的质因数乘积: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)

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849