Skip to content

Commit 1598c59

Browse files
authored
Merge pull request #26 from colorbox/49
49. Group Anagrams
2 parents afba9a0 + bfaa671 commit 1598c59

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed

49/step1.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
};

49/step2.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
};

49/step2_2.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
};

49/step2_3.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
};

49/step3.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
};

0 commit comments

Comments
 (0)