Skip to content

Commit a360b2d

Browse files
committed
Add solution #49
1 parent 4f54188 commit a360b2d

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
36|[Valid Sudoku](./0036-valid-sudoku.js)|Medium|
2121
41|[First Missing Positive](./0041-first-missing-positive.js)|Hard|
2222
43|[Multiply Strings](./0043-multiply-strings.js)|Medium|
23+
49|[Group Anagrams](./0049-group-anagrams.js)|Medium|
2324
50|[Pow(x, n)](./0050-powx-n.js)|Medium|
2425
54|[Spiral Matrix](./0054-spiral-matrix.js)|Medium|
2526
58|[Length of Last Word](./0058-length-of-last-word.js)|Easy|

solutions/0049-group-anagrams.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* 49. Group Anagrams
3+
* https://leetcode.com/problems/group-anagrams/
4+
* Difficulty: Medium
5+
*
6+
* Given an array of strings `strs`, group the anagrams together. You can return the
7+
* answer in any order.
8+
*
9+
* An Anagram is a word or phrase formed by rearranging the letters of a different
10+
* word or phrase, typically using all the original letters exactly once.
11+
*/
12+
13+
/**
14+
* @param {string[]} strs
15+
* @return {string[][]}
16+
*/
17+
var groupAnagrams = function(strs) {
18+
const map = {};
19+
20+
strs.forEach(str => {
21+
const key = [...str].sort();
22+
map[key] = map[key] ? [...map[key], str] : [str];
23+
});
24+
25+
return Object.values(map);
26+
};

0 commit comments

Comments
 (0)