(LeetCode 49)Anagrams
Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
题目:
给一组字符串,返回所有满足Anagrams(回文构词法)的字符串;
Anagrams是指由颠倒字母顺序组成的单词,比如“dormitory”颠倒字母顺序会变成“dirty room”,“tea”会变成“eat”。
回文构词法有一个特点:单词里的字母的种类和数目没有改变,只是改变了字母的排列顺序。
思路:
满足Anagrams的字符串中的字符种类和数目都一样,如果对字符串排序,那么他们必定是相等,于是可以想到通过Hash表来判断该字符串是否出现过,如果出现过,即满足Anagrams;
需要考虑的是出现三个或三个以上的判断条件,这时数组式的hash表已无法满足,因此可以采用map<string,int>,通过key-value的形式来记录;
当string没有出现过,则对应的value为字符串数组的下表i,即map[string]=i;
当string已出现过一次,则对应的value可以设为-1,并输出该对应的字符串,即map[string]=-1;
当string出现过一次以上,即map[string]==-1,则直接输出该s对应的字符串;
代码:
class Solution {
public:
vector<string> anagrams(vector<string>& strs) {
vector<string> result;
int len=strs.size();
if(len<2)
return result; map<string,int> anagrams;
string aStr;
for(int i=0;i<len;i++){
aStr=strs[i];
sort(aStr.begin(),aStr.end());
if(anagrams.find(aStr)==anagrams.end())
anagrams[aStr]=i;
else{
if(anagrams[aStr]>=0){
result.push_back(strs[anagrams[aStr]]);
anagrams[aStr]=-1;
}
result.push_back(strs[i]);
}
}
}
};
(LeetCode 49)Anagrams的更多相关文章
- (Problem 49)Prime permutations
The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual ...
- LeetCode 49 Group Anagrams(字符串分组)
题目链接: https://leetcode.com/problems/anagrams/?tab=Description Problem:给一个字符串数组,将其中的每个字符串进行分组,要求每个分 ...
- LeetCode - 49. Group Anagrams
49. Group Anagrams Problem's Link ------------------------------------------------------------------ ...
- leetcode@ [49] Group Anagrams (Hashtable)
https://leetcode.com/problems/anagrams/ Given an array of strings, group anagrams together. For exam ...
- (LeetCode 78)SubSets
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- (LeetCode 72)Edit Distance
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- 算法学习笔记(LeetCode OJ)
================================== LeetCode的一些算法题,都是自己做的,欢迎提出改进~~ LeetCode:http://oj.leetcode.com == ...
- [leetcode]49. Group Anagrams变位词归类
Given an array of strings, group anagrams together. Example: Input: ["eat", "tea" ...
- opencv配置(2.49)
转载自浅墨大神http://blog.csdn.net/poem_qianmo/article/details/19809337 OpenCV2.4.9和2.4.8的配置几乎一样,唯一的区别在下文中的 ...
随机推荐
- centos7 更改时区
Linux 系统(我特指发行版, 没说内核) 下大部分软件的风格就是不会仔细去考虑向后 的兼容性, 比如你上个版本能用这种程序配置, 没准到了下一个版本, 该程序已经不见了. 比如 sysvinit ...
- Nearest Neighbor Search
## Nearest Neighbor Search ## Input file: standard input Output file: standard output Time limit: 1 ...
- SPOJ694 DISUBSTR --- 后缀数组 / 后缀自动机
SPOJ694 DISUBSTR 题目描述: Given a string, we need to find the total number of its distinct substrings. ...
- POJ 2728 JZYZOJ 1636 分数规划 最小生成树 二分 prim
http://172.20.6.3/Problem_Show.asp?id=1636 复习了prim,分数规划大概就是把一个求最小值或最大值的分式移项变成一个可二分求解的式子. #include< ...
- 62.COUNT(递归算法)--数的划分变式题型
文件名:count.cpp 输入输出文件:count.in.count.out 时空:64M,2s 我们已经知道这样一个定理:任意一个正整数能够分解成最多4个数字的平方和.现在给你一些数字,要你求出它 ...
- bzoj1798 维护序列
Description 老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成. 有长为N的数列,不妨设为a1,a2,…,aN .有如下三种操作形式: (1)把数列中的一段数全部乘一个值; (2 ...
- Codeforces Beta Round #6 (Div. 2 Only) B. President's Office 水题
B. President's Office 题目连接: http://codeforces.com/contest/6/problem/B Description President of Berla ...
- Educational Codeforces Round 10 A. Gabriel and Caterpillar 模拟
A. Gabriel and Caterpillar 题目连接: http://www.codeforces.com/contest/652/problem/A Description The 9-t ...
- Codeforces Beta Round #4 (Div. 2 Only) B. Before an Exam dp
B. Before an Exam 题目连接: http://www.codeforces.com/contest/4/problem/B Description Tomorrow Peter has ...
- memcached添加日志输出
引子:qa的memcached总是隔一段时间挂掉,导致qa进不去.决定查一下原因,于是添加日志输出,等下次出错便于查阅.定位问题. memcache默认没有日志输出.如果想把memecache服务日志 ...