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& ...
随机推荐
- react+redux开发谷歌插件
React Developer Tools Redux Dev Tools
- 隐藏原生html5 video controls
隐藏原生html5 video controls 凤凰视频焦点项目mobile html5播放器测试时bug,由于没有用原生的controls而是自己写的custom controls,虽然设置了co ...
- monkey 自己总结
adb shell monkey -p com.test.smstest --pct-touch 50 --pct-motion 30 --pct-nav 20 -s 10 -v --throttle ...
- typeof,GetType
typeof: 是运算符,获得某一类型的 System.Type 对象. Int32 t = new Int32(); Type t = typeof(int); GetType: 是方法,获取当前实 ...
- virsh创建和恢复快照
virsh创建快照: virsh snapshot-create-as image的名称 snapshot的名称 例如: virsh snapshot-create-as 172.16.1.1_cen ...
- 【crawler】log4j:WARN No appenders could be found for logger (dao.hsqlmanager).
This Short introduction to log4j guide is a little bit old but still valid. That guide will give you ...
- Reconstruction
/* Reconstruction */ /*@mixin border($position){ @if $position == left-right or $position == ...
- JAVA08多态之课程问题解决
课后作业一:接口多态:使用接口代替抽象基类 1.源代码: package zoo4; import java.util.Vector; public class Zoo2 { public stati ...
- 非常有趣的Console
原文地址: http://www.helloweba.com/view-blog-383.html 批量去掉或替换文本中的换行符(notepad++.sublime text2) 原文地址:http: ...
- 查找增强出口和BADI程序
*&---------------------------------------------------------------------* *& Report Z_FIND_EX ...