Skip to content

Commit 8677bb3

Browse files
committed
day 12
1 parent dfbc11a commit 8677bb3

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Group Anagrams
3+
==============
4+
5+
Given an array of strings strs, group the anagrams together. You can return the answer in any order.
6+
7+
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
8+
9+
Example 1:
10+
Input: strs = ["eat","tea","tan","ate","nat","bat"]
11+
Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
12+
13+
Example 2:
14+
Input: strs = [""]
15+
Output: [[""]]
16+
17+
Example 3:
18+
Input: strs = ["a"]
19+
Output: [["a"]]
20+
21+
Constraints:
22+
1 <= strs.length <= 104
23+
0 <= strs[i].length <= 100
24+
strs[i] consists of lower-case English letters.
25+
*/
26+
27+
class Solution
28+
{
29+
public:
30+
vector<vector<string>> groupAnagrams(vector<string> &S)
31+
{
32+
unordered_map<string, vector<string>> strs;
33+
34+
for (auto &i : S)
35+
{
36+
auto sorted = i;
37+
sort(sorted.begin(), sorted.end());
38+
strs[sorted].push_back(i);
39+
}
40+
41+
vector<vector<string>> ans;
42+
for (auto &i : strs)
43+
ans.push_back(i.second);
44+
45+
return ans;
46+
}
47+
};

Leetcode Daily Challenge/August-2021/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
| 7. | [Palindrome Partitioning II](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3872/) | [cpp](./07.%20Palindrome%20Partitioning%20II.cpp) |
1313
| 9. | [Add Strings](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/614/week-2-august-8th-august-14th/3875/) | [cpp](./09.%20Add%20Strings.cpp) |
1414
| 10. | [Flip String to Monotone Increasing](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/614/week-2-august-8th-august-14th/3876/) | [cpp](./10.%20Flip%20String%20to%20Monotone%20Increasing.cpp) |
15+
| 12. | [Group Anagrams](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/614/week-2-august-8th-august-14th/3887/) | [cpp](./12.%20Group%20Anagrams.cpp) |

0 commit comments

Comments
 (0)