File tree Expand file tree Collapse file tree 5 files changed +104
-0
lines changed Expand file tree Collapse file tree 5 files changed +104
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ Time: 7:07
3
+
4
+ Time: O(N * M log M)
5
+ Space: O(N * M)
6
+ */
7
+ class Solution {
8
+ public:
9
+ vector<vector<string>> groupAnagrams (vector<string>& strs) {
10
+ map<string, vector<string>> sorted_to_str;
11
+ for (auto str: strs) {
12
+ string key = str;
13
+ sort (key.begin (), key.end ());
14
+ sorted_to_str[key].push_back (str);
15
+ }
16
+ vector<vector<string>>grouped_anagrams;
17
+ for (auto [key, group]: sorted_to_str) {
18
+ grouped_anagrams.push_back (group);
19
+ }
20
+ return grouped_anagrams;
21
+ }
22
+ };
Original file line number Diff line number Diff line change
1
+ /*
2
+ 無駄なコピーをなくして空間計算量を改善。
3
+ */
4
+ class Solution {
5
+ public:
6
+ vector<vector<string>> groupAnagrams (vector<string>& strs) {
7
+ map<string, vector<string>> sorted_to_str;
8
+ for (auto & str: strs) {
9
+ string key = str;
10
+ sort (key.begin (), key.end ());
11
+ sorted_to_str[key].push_back (str);
12
+ }
13
+ vector<vector<string>>grouped_anagrams;
14
+ for (auto & [key, group]: sorted_to_str) {
15
+ grouped_anagrams.push_back (move (group));
16
+ }
17
+ return grouped_anagrams;
18
+ }
19
+ };
Original file line number Diff line number Diff line change
1
+ /*
2
+ sortを利用せずに時間計算量を改善
3
+ */
4
+ class Solution {
5
+ public:
6
+ vector<vector<string>> groupAnagrams (vector<string>& strs) {
7
+ map<vector<int >, vector<string>> sorted_to_str;
8
+ for (auto & str: strs) {
9
+ vector<int > keys (26 );
10
+ for (char c : str) {
11
+ keys[c - ' a' ]++;
12
+ }
13
+ sorted_to_str[keys].push_back (str);
14
+ }
15
+ vector<vector<string>>grouped_anagrams;
16
+ for (auto & [key, group]: sorted_to_str) {
17
+ grouped_anagrams.push_back (move (group));
18
+ }
19
+ return grouped_anagrams;
20
+ }
21
+ };
Original file line number Diff line number Diff line change
1
+ /*
2
+ 文字列構築にstd::ostringstreamを使う
3
+ */
4
+ class Solution {
5
+ public:
6
+ vector<vector<string>> groupAnagrams (vector<string>& strs) {
7
+ map<string, vector<string>> sorted_to_str;
8
+ for (auto & str: strs) {
9
+ vector<int > counter (26 );
10
+ for (char c : str) {
11
+ counter[c - ' a' ]++;
12
+ }
13
+ std::ostringstream key;
14
+ for (auto & count : counter) {
15
+ key << " #" << to_string (count);
16
+ }
17
+
18
+ sorted_to_str[key.str ()].push_back (str);
19
+ }
20
+ vector<vector<string>>grouped_anagrams;
21
+ for (auto & [key, group]: sorted_to_str) {
22
+ grouped_anagrams.push_back (move (group));
23
+ }
24
+ return grouped_anagrams;
25
+ }
26
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ vector<vector<string>> groupAnagrams (vector<string>& strs) {
4
+ map<string, vector<string>> sorted_to_strings;
5
+ for (auto & str : strs) {
6
+ string key = str;
7
+ sort (key.begin (), key.end ());
8
+ sorted_to_strings[key].push_back (str);
9
+ }
10
+ vector<vector<string>> grouped_anagrams;
11
+ for (auto & [key, strings] : sorted_to_strings) {
12
+ grouped_anagrams.push_back (move (strings));
13
+ }
14
+ return grouped_anagrams;
15
+ }
16
+ };
You can’t perform that action at this time.
0 commit comments