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的更多相关文章

  1. (Problem 49)Prime permutations

    The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual ...

  2. LeetCode 49 Group Anagrams(字符串分组)

    题目链接: https://leetcode.com/problems/anagrams/?tab=Description   Problem:给一个字符串数组,将其中的每个字符串进行分组,要求每个分 ...

  3. LeetCode - 49. Group Anagrams

    49. Group Anagrams Problem's Link ------------------------------------------------------------------ ...

  4. leetcode@ [49] Group Anagrams (Hashtable)

    https://leetcode.com/problems/anagrams/ Given an array of strings, group anagrams together. For exam ...

  5. (LeetCode 78)SubSets

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

  6. (LeetCode 72)Edit Distance

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  7. 算法学习笔记(LeetCode OJ)

    ================================== LeetCode的一些算法题,都是自己做的,欢迎提出改进~~ LeetCode:http://oj.leetcode.com == ...

  8. [leetcode]49. Group Anagrams变位词归类

    Given an array of strings, group anagrams together. Example: Input: ["eat", "tea" ...

  9. opencv配置(2.49)

    转载自浅墨大神http://blog.csdn.net/poem_qianmo/article/details/19809337 OpenCV2.4.9和2.4.8的配置几乎一样,唯一的区别在下文中的 ...

随机推荐

  1. Android升级ADT22后会报ClassNotFoundException的原因分析

    http://blog.csdn.net/huzgd/article/details/8962702 1.ADT16下,只要add to path就是add to path并export:2.ADT2 ...

  2. onvif 开发中的一些重要函数介绍

    ➤soap结构中count(soap->count)成员 soap结构中count(soap->count)成员记录的是http协议中Content-Length的数值. ➤keep_al ...

  3. 【UOJ #105】【APIO2014】Beads and wires

    http://uoj.ac/problem/105 好神的dp啊. 确定一个点为根之后,蓝线只能是竖着的,不能横跨兄弟. 枚举每个点为根进行树形dp是\(O(n^2)\)的,\(f(x,0/1)\)表 ...

  4. Codeforces 1103 C. Johnny Solving

    Codeforces 1103 C. Johnny Solving 题目大意: 有一张 \(n\) 个点 \(m\) 条边的简单无向图,每个点的度数至少为 \(3\) ,你需要构造出两种情况之一 一条 ...

  5. Android演示Stack(课下作业)

    Demand 之前活动中误传成别的截图,故在此补充博客 1.使用自己实现的栈构建Android程序,提供用于栈的一个puh按钮和pop按钮,在文本域接收一个字符串作为push的输入,文本区将显示每个操 ...

  6. Ubuntu 12.04下spark1.0.0 集群搭建(原创)

    spark1.0.0新版本的于2014-05-30正式发布啦,新的spark版本带来了很多新的特性,提供了更好的API支持,spark1.0.0增加了Spark SQL组件,增强了标准库(ML.str ...

  7. [转]jquery加载页面的方法(页面加载完成就执行)

    jquery加载页面的方法(页面加载完成就执行),建议大家看下windows.onload与$(document).ready之间的区别.   1.$(function(){ $("#a&q ...

  8. April Fools Day Contest 2016 E. Out of Controls

    E. Out of Controls 题目连接: http://www.codeforces.com/contest/656/problem/E Description You are given a ...

  9. leetcode47. Permutations II

    leetcode47. Permutations II 题意: 给定可能包含重复的数字的集合,返回所有可能的唯一排列. 思路: 这题全排列两种写法. 用hash表记录已经visit的数字,虽然看起来很 ...

  10. 前端UED网站汇总

    爱词霸UED团队 MED | 营销展现研究专家 携程UED-携程旅行前端开发团队 支付宝前端开发车间 Taobao UED Team: 淘宝网用户体验团队博客,有关用户体验设计和研究的经验分享. - ...