Given an array of strings, return all groups of strings that are anagrams.

Notice

All inputs will be in lower-case

 
Example

Given ["lint", "intl", "inlt", "code"], return ["lint", "inlt", "intl"].

Given ["ab", "ba", "cd", "dc", "e"], return ["ab", "ba", "cd", "dc"].

Challenge

What is Anagram?
- Two strings are anagram if they can be the same after change the order of characters.

解法一:

 class Solution {
public:
/**
* @param strs: A list of strings
* @return: A list of strings
*/
string getSortedString(string &str) {
static int count[];
for (int i = ; i < ; i++) {
count[i] = ;
}
for (int i = ; i < str.length(); i++) {
count[str[i] - 'a']++;
} string sorted_str = "";
for (int i = ; i < ; i++) {
for (int j = ; j < count[i]; j++) {
sorted_str = sorted_str + (char)('a' + i);
}
}
return sorted_str;
} vector<string> anagrams(vector<string> &strs) {
unordered_map<string, int> hash; for (int i = ; i < strs.size(); i++) {
string str = getSortedString(strs[i]);
if (hash.find(str) == hash.end()) {
hash[str] = ;
} else {
hash[str] = hash[str] + ;
}
} vector<string> result;
for (int i = ; i < strs.size(); i++) {
string str = getSortedString(strs[i]);
if (hash.find(str) == hash.end()) {
continue;
} else {
if (hash[str] > ) {
result.push_back(strs[i]);
}
}
}
return result;
}
};

手动实现了字符串的排序,有些复杂,参考@NineChapter 的代码

解法二:

 class Solution {
public:
/**
* @param strs: A list of strings
* @return: A list of strings
*/
vector<string> anagrams(vector<string> &strs) {
int size = strs.size(), i = ;
if (size <= ) {
return vector<string>();
} vector<string> result;
map<string, int> hash;
for (i = ; i < size; i++) {
string temp = strs[i];
sort(temp.begin(), temp.end());
hash[temp]++;
}
for (i = ; i < size; i++) {
string temp = strs[i];
sort(temp.begin(), temp.end());
if (hash[temp] > ) {
result.push_back(strs[i]);
}
}
return result;
}
};

直接用STL的sort函数搞

171. Anagrams【medium】的更多相关文章

  1. 2. Add Two Numbers【medium】

    2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...

  2. 92. Reverse Linked List II【Medium】

    92. Reverse Linked List II[Medium] Reverse a linked list from position m to n. Do it in-place and in ...

  3. 82. Remove Duplicates from Sorted List II【Medium】

    82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...

  4. 61. Search for a Range【medium】

    61. Search for a Range[medium] Given a sorted array of n integers, find the starting and ending posi ...

  5. 62. Search in Rotated Sorted Array【medium】

    62. Search in Rotated Sorted Array[medium] Suppose a sorted array is rotated at some pivot unknown t ...

  6. 74. First Bad Version 【medium】

    74. First Bad Version [medium] The code base version is an integer start from 1 to n. One day, someo ...

  7. 75. Find Peak Element 【medium】

    75. Find Peak Element [medium] There is an integer array which has the following features: The numbe ...

  8. 159. Find Minimum in Rotated Sorted Array 【medium】

    159. Find Minimum in Rotated Sorted Array [medium] Suppose a sorted array is rotated at some pivot u ...

  9. Java for LeetCode 207 Course Schedule【Medium】

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

随机推荐

  1. appium+python自动化50-生成定位对象模板templet(jinja2)

    前言 每次自己写pageobject定位元素对象太繁琐,格式都差不多,只是换个定位方法,这种就可以才有模板的方式,批量生成pageobject定位元素对象的模板 python里面生成模板有两个模块可以 ...

  2. [Android Pro] 创建快捷方式,删除快捷方式,查询是否存在快捷方式

    1: 创建快捷方式 需要权限: <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORT ...

  3. [转] SSIS可靠性和扩展性—事务

    本文转自:http://www.cnblogs.com/tylerdonet/archive/2011/09/23/2186579.html 在这一个随笔中将介绍在package中如何使用事务来保证数 ...

  4. 安装red5 1.0.1版本Java_home不能用Java7

    安装red5     1.0.1一直出现问题,安装顺利可以过,但是一访问老是报错. 用1.0之前的版本则没有问题.好一顿折腾,查看log发现问题出在tomcat 的nio上,查询这个问题有回复说是jr ...

  5. Python绘制直方图 Pygal模拟掷骰子

    #coding=utf-8 from random import randint class Die(): """骰子类""" def __ ...

  6. RocketMQ概念整理

    DefaultMessageStore 消息的存储和提取. 相对重要的两个方法: 消息存储 PutMessageResult putMessage(MessageExtBrokerInner msg) ...

  7. 工程web-inf 下文件,路径访问

    直接用相对路径../即可 效果:

  8. 使用Sublime经验分享

    Sublime轻量级.可是绝对是神一样的编辑器. 1.代码清晰美观 2.能够选择文件夹作为文件结构文件夹显示在左側 3.以tab的形式打开多个页面在同一个窗体内 设置方法例如以下: Preferenc ...

  9. 系统找不到指定文件 No installed service name 'Apache2'

    原因:系统服务中没有apache2服务 解决方法: 开始 --运行 --- 输入“CMD”出来DOS窗口---- 输入 D: 回车 再输入 cd D:/Program Files(x86)/Apach ...

  10. WIN7如何查找网络打印机

    1 在开始菜单中输入"打印机"并点击"添加打印机" 2 点击下面一个,并搜索家庭组的打印机,一般可以搜到(注意这台电脑不能关机或睡眠). 3 查找并添加会需要安 ...