-
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
Conversation
hayashi-ay
left a comment
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.
良いと思います。
| public: | ||
| vector<vector<string>> groupAnagrams(vector<string>& strs) { | ||
| map<string, vector<string>> sorted_to_str; | ||
| for (auto str: strs) { |
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.
ありがとうございます、こちらのほうが良さそうですね。
| 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Modernの書き方だと以下よく見るのでご参考程度に!
transform(begin(sorted_to_strings), end(sorted_to_strings), back_inserter(grouped_anagrams), [](const auto& groups) {
return groups.second;
});
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.
C++ 17 からですか。
https://en.cppreference.com/w/cpp/algorithm/transform
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.
なるほど、ありがとうございます。参考にします。
| 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 comment
The reason will be displayed to describe this comment to others. Learn more.
これ、仮に str に小文字のアルファベットでないものが入っていたら何が起きるでしょうか。
入力の validation はどれくらいするべきだと思いますか。(これはユースケース次第なのでユースケースを適当にでっちあげて考えてみて下さい。)
仮に validate するとしたらどのような動作が望ましいでしょうか。(これもユースケース次第でしょうし唯一の正解があるわけではありません。)
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.
これ、仮に str に小文字のアルファベットでないものが入っていたら何が起きるでしょうか。
vectorの範囲外の参照でエラーで落ちますね
入力の validation はどれくらいするべきだと思いますか。(これはユースケース次第なのでユースケースを適当にでっちあげて考えてみて下さい。)
各文字列の文字がa-zの範囲内にあることは確認すべきかなと思います。
仮に validate するとしたらどのような動作が望ましいでしょうか。(これもユースケース次第でしょうし唯一の正解があるわけではありません。)
文字のチェックはいくつかやり方あると思いますが一文字ずつループで回してc - 'a'をチェックする、strに正規表現を使うなどが考えられそうです。
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.
.at と [] で振る舞いが違い、[] のほうは未定義動作であるというのはいいですか。
C++ の未定義動作という概念はありますか。
書き方はループでチェックでいいんですが、チェックした結果どう振る舞うのが好ましいですか。
Exception を投げる、プログラムが終了する、特殊な値を返す、単に取り除いた結果を返すなどです。
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.
未定義動作については知っていました、なるべく発生させるべきでないものという浅い理解でした。
結果に対する振る舞いについては、Exceptionを投げて呼び出し側で対応するのが良いと思います。
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.
未定義動作は、何が起きても構わない、ということです。
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.
未定義動作は絶対避けるように気をつけます。
https://ja.cppreference.com/w/cpp/language/ub
| 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 comment
The reason will be displayed to describe this comment to others. Learn more.
こちらも、値のコピーを避けるため const auto& [key, group] としたほうがよいと思います。
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&を利用すべきときは利用します。
| 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 comment
The reason will be displayed to describe this comment to others. Learn more.
vector<vector<string>> と grouped_anagrams; のあいだにスペースを 1 つ空けることをおすすめいたします。
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.
ありがとうございます、気をつけます。
| } | ||
| std::ostringstream key; | ||
| for (auto& count : counter) { | ||
| key << "#" << to_string(count); |
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.
すいません この"#"ってなんですか??
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.
各文字の出現回数を文字列上で区切るためのセパレーターですね。
| counter[c - 'a']++; | ||
| } | ||
| std::ostringstream key; | ||
| for (auto& count : counter) { |
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.
intなら参照にする必要はないでしょう。
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.
ありがとうございます、たしかにこれは不要な&をつけてしまっていました。
49. Group Anagrams
kazukiii/leetcode#13
rihib/leetcode#3
Mike0121/LeetCode#29
Ryotaro25/leetcode_first60#13
TORUS0818/leetcode#14
fhiyo/leetcode#15
Exzrgs/LeetCode#26
nittoco/leetcode#13
t-ooka/leetcode#2
SuperHotDogCat/coding-interview#13
ryoooooory/LeetCode#1
sakupan102/arai60-practice#13
goto-untrapped/Arai60#6
YukiMichishita/LeetCode#5
hayashi-ay/leetcode#19
Yoshiki-Iwasa/Arai60#11
rihib/leetcode#3
seal-azarashi/leetcode#12
hroc135/leetcode#12