Anagrams
这题Leetcode上面的描述不清楚。怎么也得举两个例子吧,不然谁懂?
题目的意思是,给定一些字符串,比如["abc","cba","bac","abcd"],找出可以通过交换位置获得的所有字符串。那么这个例子中,返回的结果就是["abc","cba","bac"]。题目隐藏了一个假设,也就是只有一组这样的结果。
理解了题目的意思的话,其实非常简单:遍历字符串,为字符相同的字符串生成一个key,放到map中。key相同的就是可以通过互换字符位置变换而来的。那么key怎么生成呢?最简单的,就是对字符串按照字母表升序进行排序。那么"abc","cba","bac"都对应"abc"这一个key。当然,对map的处理,要进行一些判断来区分第一次访问、已访问等。代码如下:
class Solution {
public:
vector<string> anagrams(vector<string> &strs) {
map<string,int> posMap;
vector<string> res;
string tmp;
for(int i=;i<strs.size();i++)
{
tmp=strs[i];
sort(tmp.begin(),tmp.end());
if(posMap.find(tmp)==posMap.end())
{
posMap[tmp]=i;
}
else
{
if(posMap[i]==-)
{
res.push_back(strs[i]);
}
else
{
res.push_back(posMap[tmp]);
res.push_back(strs[i]);
posMap[tmp]=-;
}
}
}
return res;
}
};
这里生成key是用普通的sort方法,复杂度Nlog(N)。由于字符是小写字母,只有26个,因此可以采用计数排序,复杂度可以降到log(N)。(这里的N是指字符数)。
Anagrams的更多相关文章
- C语言 · Anagrams问题
问题描述 Anagrams指的是具有如下特性的两个单词:在这两个单词当中,每一个英文字母(不区分大小写)所出现的次数都是相同的.例如,"Unclear"和"Nuclear ...
- [LeetCode] Find All Anagrams in a String 找出字符串中所有的变位词
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...
- [LeetCode] Anagrams 错位词
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
- Leetcode Anagrams
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
- LeetCode Find All Anagrams in a String
原题链接在这里:https://leetcode.com/problems/find-all-anagrams-in-a-string/ 题目: Given a string s and a non- ...
- LintCode Anagrams
(记得import java.util.HashMap及Arrays, 首先字符串若为空或者数量为零, 则返回一个空的LinkedList) 1. 把string变为char数组, 再进行排序, 之后 ...
- LeetCode - 49. Group Anagrams
49. Group Anagrams Problem's Link ------------------------------------------------------------------ ...
- Two Strings Are Anagrams
Write a method anagram(s,t) to decide if two strings are anagrams or not. 判断两个字符串里的字符是否相同,也就是是否能够通过改 ...
- Group Anagrams
Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...
随机推荐
- Web开发中20个很有用的CSS库
来源: 微信公众号文章 在过去的几年中,CSS已经成为一大部分开发者和设计者的最爱,因为它提供了一系列功能和特性.每个月都有无数个围绕CSS的工具被开发者发布以简化WEB开发.像CSS库,框架,应用这 ...
- jquery总结06-动画事件01-基础显示和隐藏
动画事件 .hide(option) 动画隐藏 会保存元素的原始属性值 $("#a2").hide({ duration: 3000, complete: function() ...
- Mac系统下显示和隐藏文件
在配置Maven的时候需要更改.m2文件,所以要将.m2显示出来,记录一下方便日后使用. 显示文件:在终端输入defaults write com.apple.finder AppleShowAllF ...
- [poj1182]食物链(并查集+补集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 64841 Accepted: 19077 Description ...
- 使用扫描二维码打开app
应该不少人遇到过这种需求,扫描二维码打开app如果用户没有这个app则提示它跳转. 用网页直接来调用app是不打可能的,必须原生那边先做一些配置. 首先,安卓和苹果的调用方法是不同的. 所以我们需要先 ...
- 参考__MySql
博客 三范式 事务隔离级别 列表
- C++ STL vector容器学习
STL(Standard Template Library)标准模板库是C++最重要的组成部分,它提供了一组表示容器.迭代器.函数对象和算法的模板.其中容器是存储类型相同的数据的结构(如vector, ...
- 初识Polymer框架
什么是polymer? polymer由谷歌的Palm webOS团队打造,并在2013 Google I/O大会上推出,旨在实现Web Components,用最少的代码,解除框架间的限制的UI 框 ...
- js判断中文
var reg = /^[\u4E00-\u9FA5]+$/;if(!reg.test(keywordscn)){ alert('请填写中文') return false;}
- Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio » 更新关系数据
Updating related data¶ 7 of 7 people found this helpful The Contoso University sample web applicatio ...