Skip to content

Conversation

Copy link

@hayashi-ay hayashi-ay left a 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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const auto&

Copy link
Owner Author

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) {

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;
});

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner Author

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']++;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これ、仮に str に小文字のアルファベットでないものが入っていたら何が起きるでしょうか。
入力の validation はどれくらいするべきだと思いますか。(これはユースケース次第なのでユースケースを適当にでっちあげて考えてみて下さい。)
仮に validate するとしたらどのような動作が望ましいでしょうか。(これもユースケース次第でしょうし唯一の正解があるわけではありません。)

Copy link
Owner Author

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に正規表現を使うなどが考えられそうです。

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.at と [] で振る舞いが違い、[] のほうは未定義動作であるというのはいいですか。
C++ の未定義動作という概念はありますか。

書き方はループでチェックでいいんですが、チェックした結果どう振る舞うのが好ましいですか。
Exception を投げる、プログラムが終了する、特殊な値を返す、単に取り除いた結果を返すなどです。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

未定義動作については知っていました、なるべく発生させるべきでないものという浅い理解でした。

結果に対する振る舞いについては、Exceptionを投げて呼び出し側で対応するのが良いと思います。

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

未定義動作は、何が起きても構わない、ということです。

Copy link
Owner Author

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) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こちらも、値のコピーを避けるため const auto& [key, group] としたほうがよいと思います。

Copy link
Owner Author

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;
Copy link

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 つ空けることをおすすめいたします。

Copy link
Owner Author

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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

すいません この"#"ってなんですか??

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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

intなら参照にする必要はないでしょう。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます、たしかにこれは不要な&をつけてしまっていました。

@colorbox colorbox merged commit 1598c59 into main Jan 19, 2025
@colorbox colorbox deleted the 49 branch January 19, 2025 14:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants