-
Notifications
You must be signed in to change notification settings - Fork 0
49. Group Anagrams #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /* | ||
| Time: 7:07 | ||
|
|
||
| Time: O(N * M log M) | ||
| Space: O(N * M) | ||
| */ | ||
| class Solution { | ||
| public: | ||
| vector<vector<string>> groupAnagrams(vector<string>& strs) { | ||
| map<string, vector<string>> sorted_to_str; | ||
| for (auto str: strs) { | ||
| string key = str; | ||
| sort(key.begin(), key.end()); | ||
| sorted_to_str[key].push_back(str); | ||
| } | ||
| vector<vector<string>>grouped_anagrams; | ||
| for (auto [key, group]: sorted_to_str) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. こちらも、値のコピーを避けるため
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. なるほど、ありがとうございます。 |
||
| grouped_anagrams.push_back(group); | ||
| } | ||
| return grouped_anagrams; | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| /* | ||
| 無駄なコピーをなくして空間計算量を改善。 | ||
| */ | ||
| class Solution { | ||
| public: | ||
| vector<vector<string>> groupAnagrams(vector<string>& strs) { | ||
| map<string, vector<string>> sorted_to_str; | ||
| for (auto& str: strs) { | ||
| string key = str; | ||
| sort(key.begin(), key.end()); | ||
| sorted_to_str[key].push_back(str); | ||
| } | ||
| vector<vector<string>>grouped_anagrams; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ありがとうございます、気をつけます。 |
||
| for (auto& [key, group]: sorted_to_str) { | ||
| grouped_anagrams.push_back(move(group)); | ||
| } | ||
| return grouped_anagrams; | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /* | ||
| sortを利用せずに時間計算量を改善 | ||
| */ | ||
| class Solution { | ||
| public: | ||
| vector<vector<string>> groupAnagrams(vector<string>& strs) { | ||
| map<vector<int>, vector<string>> sorted_to_str; | ||
| for (auto& str: strs) { | ||
| vector<int> keys(26); | ||
| for (char c : str) { | ||
| keys[c - 'a']++; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. これ、仮に str に小文字のアルファベットでないものが入っていたら何が起きるでしょうか。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
vectorの範囲外の参照でエラーで落ちますね
各文字列の文字が
文字のチェックはいくつかやり方あると思いますが一文字ずつループで回して There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. .at と [] で振る舞いが違い、[] のほうは未定義動作であるというのはいいですか。 書き方はループでチェックでいいんですが、チェックした結果どう振る舞うのが好ましいですか。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 未定義動作については知っていました、なるべく発生させるべきでないものという浅い理解でした。 結果に対する振る舞いについては、Exceptionを投げて呼び出し側で対応するのが良いと思います。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 未定義動作は、何が起きても構わない、ということです。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 未定義動作は絶対避けるように気をつけます。 |
||
| } | ||
| sorted_to_str[keys].push_back(str); | ||
| } | ||
| vector<vector<string>>grouped_anagrams; | ||
| for (auto& [key, group]: sorted_to_str) { | ||
| grouped_anagrams.push_back(move(group)); | ||
| } | ||
| return grouped_anagrams; | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /* | ||
| 文字列構築にstd::ostringstreamを使う | ||
| */ | ||
| class Solution { | ||
| public: | ||
| vector<vector<string>> groupAnagrams(vector<string>& strs) { | ||
| map<string, vector<string>> sorted_to_str; | ||
| for (auto& str: strs) { | ||
| vector<int> counter(26); | ||
| for (char c : str) { | ||
| counter[c - 'a']++; | ||
| } | ||
| std::ostringstream key; | ||
| for (auto& count : counter) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. intなら参照にする必要はないでしょう。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ありがとうございます、たしかにこれは不要な&をつけてしまっていました。 |
||
| key << "#" << to_string(count); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. すいません この"#"ってなんですか?? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 各文字の出現回数を文字列上で区切るためのセパレーターですね。 |
||
| } | ||
|
|
||
| sorted_to_str[key.str()].push_back(str); | ||
| } | ||
| vector<vector<string>>grouped_anagrams; | ||
| for (auto& [key, group]: sorted_to_str) { | ||
| grouped_anagrams.push_back(move(group)); | ||
| } | ||
| return grouped_anagrams; | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| class Solution { | ||
| public: | ||
| vector<vector<string>> groupAnagrams(vector<string>& strs) { | ||
| map<string, vector<string>> sorted_to_strings; | ||
| for (auto& str : strs) { | ||
| string key = str; | ||
| sort(key.begin(), key.end()); | ||
| sorted_to_strings[key].push_back(str); | ||
| } | ||
| vector<vector<string>> grouped_anagrams; | ||
| for (auto& [key, strings] : sorted_to_strings) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C++ 17 からですか。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. なるほど、ありがとうございます。参考にします。 |
||
| grouped_anagrams.push_back(move(strings)); | ||
| } | ||
| return grouped_anagrams; | ||
| } | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const auto&
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます、こちらのほうが良さそうですね。