49:字母异位词分组

This commit is contained in:
huangge1199 2021-09-03 09:41:17 +08:00
parent a242551365
commit 7f63430509
3 changed files with 107 additions and 1 deletions

View File

@ -0,0 +1,71 @@
//给你一个字符串数组请你将 字母异位词 组合在一起可以按任意顺序返回结果列表
//
// 字母异位词 是由重新排列源单词的字母得到的一个新单词所有源单词中的字母都恰好只用一次
//
//
//
// 示例 1:
//
//
//输入: strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
//输出: [["bat"],["nat","tan"],["ate","eat","tea"]]
//
// 示例 2:
//
//
//输入: strs = [""]
//输出: [[""]]
//
//
// 示例 3:
//
//
//输入: strs = ["a"]
//输出: [["a"]]
//
//
//
// 提示
//
//
// 1 <= strs.length <= 10
// 0 <= strs[i].length <= 100
// strs[i] 仅包含小写字母
//
// Related Topics 哈希表 字符串 排序 👍 836 👎 0
package leetcode.editor.cn;
import java.util.*;
//49:字母异位词分组
class GroupAnagrams{
public static void main(String[] args) {
//测试代码
Solution solution = new GroupAnagrams().new Solution();
solution.groupAnagrams(new String[]{"eat","tea","tan","ate","nat","bat"});
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
Map<String,List<String>> map = new HashMap<>();
for(String s:strs){
char[] chs = s.toCharArray();
Arrays.sort(chs);
String str = Arrays.toString(chs);
List<String> list = map.getOrDefault(str,new ArrayList<>());
list.add(s);
map.put(str,new ArrayList<>(list));
}
List<List<String>> result = new ArrayList<>();
for (String key:map.keySet()){
result.add(new ArrayList<>(map.get(key)));
}
return result;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,35 @@
<p>给你一个字符串数组,请你将 <strong>字母异位词</strong> 组合在一起。可以按任意顺序返回结果列表。</p>
<p><strong>字母异位词</strong> 是由重新排列源单词的字母得到的一个新单词,所有源单词中的字母都恰好只用一次。</p>
<p>&nbsp;</p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong> strs = <code>["eat", "tea", "tan", "ate", "nat", "bat"]</code>
<strong>输出: </strong>[["bat"],["nat","tan"],["ate","eat","tea"]]</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong> strs = <code>[""]</code>
<strong>输出: </strong>[[""]]
</pre>
<p><strong>示例 3:</strong></p>
<pre>
<strong>输入:</strong> strs = <code>["a"]</code>
<strong>输出: </strong>[["a"]]</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= strs.length &lt;= 10<sup>4</sup></code></li>
<li><code>0 &lt;= strs[i].length &lt;= 100</code></li>
<li><code>strs[i]</code>&nbsp;仅包含小写字母</li>
</ul>
<div><div>Related Topics</div><div><li>哈希表</li><li>字符串</li><li>排序</li></div></div><br><div><li>👍 836</li><li>👎 0</li></div>