Skip to content

Commit

Permalink
feat(leecode): 49.字母异位词分组
Browse files Browse the repository at this point in the history
  • Loading branch information
z979054461 committed Mar 23, 2024
1 parent bae9ec5 commit 7768562
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/10.LeeCode/48.旋转图像.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ Langs: c cpp csharp dart golang java javascript kotlin php python python3 racket
<p>&nbsp;</p>

<<< @/src/LeeCode/48.旋转图像.js
<<< @/src/LeeCode/48.旋转图像 1.js
<<< @/src/LeeCode/48.旋转图像1.js
65 changes: 65 additions & 0 deletions docs/10.LeeCode/49.字母异位词分组.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
title: 49.字母异位词分组
date: 2024-03-23
lang: 'zh-CN'
sidebar: 'auto'
categories:
- LeeCode
tags:
location: HangZhou
---

# Heading

[[toc]]

[49.字母异位词分组](https://leetcode.cn/problems/group-anagrams/description/)

Tags: algorithms amazon bloomberg facebook uber yelp hash-table string

Langs: c cpp csharp dart elixir erlang golang java javascript kotlin php python python3 racket ruby rust scala swift typescript

- algorithms
- Medium (67.90%)
- Likes: 1857
- Dislikes: -
- Total Accepted: 657.2K
- Total Submissions: 967.4K
- Testcase Example: '["eat","tea","tan","ate","nat","bat"]'

<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>

<<< @/src/LeeCode/49.字母异位词分组.js
22 changes: 22 additions & 0 deletions src/LeeCode/49.字母异位词分组.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* @lc app=leetcode.cn id=49 lang=javascript
*
* [49] 字母异位词分组
*/

// @lc code=start
/**
* @param {string[]} strs
* @return {string[][]}
*/
var groupAnagrams = function (strs) {
const map = new Map()

for (let str of strs) {
const key = str.split('').sort().join()
map.set(key, map.has(key) ? [...map.get(key), str] : [str])
}

return [...map.values()]
}
// @lc code=end

0 comments on commit 7768562

Please sign in to comment.