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. 万里长征第二步——django个人博客(第二步 ——日志记录器)

    定义日志记录器 可以在setting.py里设置日志记录器 # 自定义日志输出信息 LOGGING = { 'version': 1, 'disable_existing_loggers': True ...

  2. jQuery中的动画与效果

    1.基本效果 匹配元素从左上角开始变浓变大或缩小到左上角变淡变小 ①隐藏元素 除了可以设置匹配元素的display:none外,可以用以下函数 hide(speed,[callback])  返回值: ...

  3. Redis的5个常见使用场景

    1.会话缓存(Session Cache) 最常用的一种使用Redis的情景是会话缓存(session cache).用Redis缓存会话比其他存储(如Memcached)的优势在于:Redis提供持 ...

  4. shader 变体variants

    https://blogs.unity3d.com/cn/2018/05/14/stripping-scriptable-shader-variants/ variants涉及的是build时间和da ...

  5. http://www.blogjava.net/xylz/archive/2010/07/08/325587.html

    http://www.blogjava.net/xylz/archive/2010/07/08/325587.html

  6. 转:关于Android机型适配这件小事儿

    http://www.umindex.com/devices/android_resolutions 大家都知道Android机型分裂严重,在开发Android App的时候永远都面临适配N多机型的问 ...

  7. 与Xamarin.Forms跨平台的用户界面

    Xamarin.Forms 与Xamarin.Forms跨平台的用户界面 Xamarin的. 形式是一个跨平台的UI工具包,它允许开发人员 轻松地创建本地用户界面布局,可以共享 在Android,iO ...

  8. zabbix_LAMP源码安装

    Zabbix源码包安装 Cenos5.3 Basic server 安装顺序 Libxml2 Libmcrypt Zlib Libpng Jpeg:需要创建目录jpeg /bin /lib /incl ...

  9. WORD中无损复制图片

    问题 默认 Ctrl+C复制出来图片图片的严重模糊,复制出来的不是原图片!因为图片尺寸被修改后复制出来的则是模糊的 解决办法 解决办法把WORD中的图片恢复成默认的,如果对图片进行了缩放请把缩放比恢复 ...

  10. tomcat中server.xml配置详解(转载)(二)

    转载自:https://www.cnblogs.com/starhu/p/5599773.html 一:<Connector>元素 由Connector接口定义.<Connector ...