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& ...
随机推荐
- App的启动过程
App的启动过程 所有的app都是通过launcher去启动的 launcher自己也是一个app,一个系统级别的app,放在asystem/app/下,使用系统签名. 对代码进行分析
- mysql基本命令整理
1.replace into(insert into 的增强版) replace into tbl_name(col_name, ...) values(...)replace into tbl_na ...
- 【学习笔记】在原生javascript中使用ActiveX和插件
什么是插件 现在的浏览器提供了大量的内置功能,但仍然有一些工作无法完成,如播放音频和视频.插件及其扩展浏览器功能就尤为重要. 插件是可下载的应用程序,可以插入到浏览器中,现在有很多不同的插件,常用的有 ...
- html4,xhtml,html5发展历史
SGML SGML 是一种很强大但很复杂的标记语言,HTML.XML 就是从中衍生出来的.SGML 的例子如下:<QUOTE TYPE="example"> typic ...
- 转:OSGI 实战 Equinox
http://download.csdn.net/detail/ifmliuzhen/3231590
- 一句话简单理解javascript中的原型对象
通过构造函数F创建的对象实例p 这个对象p的原型对象是 构造函数中prototype属性指向的对象s,这个对象p中也有个非标准的__proto__属性指向构造函数prototype属性所指向的对象s, ...
- 小程序 web 端实时运行工具
微信小程序 web 端实时运行工具 https://chemzqm.github.io/wept/
- .net 导出Excel功能
将DataSet对象导出成Excel文档 一.不带格式控制 void btnExport_Click(object sender, EventArgs e) { IList<string> ...
- jquery获得option的值和对option进行操作
Query获取Select元素,并选择的Text和Value: $("#select_id").change(function(){//code...}); //为Select添加 ...
- FullPage.js – 轻松实现全屏滚动(单页网站)效果
FullPage.js 是一个简单而易于使用的插件,用来创建全屏滚动网站(也被称为单页网站).除了可以创建全屏滚动效果以外,也可以给网站添加一些水平的滑块效果.能够自适应不同的屏幕尺寸,包括平板电脑和 ...