Skip to content

Create 49. Group Anagrams#24

Open
Apo-Matchbox wants to merge 1 commit intomainfrom
49.-Group-Anagrams
Open

Create 49. Group Anagrams#24
Apo-Matchbox wants to merge 1 commit intomainfrom
49.-Group-Anagrams

Conversation

@Apo-Matchbox
Copy link
Owner

Added detailed explanation and example code for grouping anagrams.

class Solution {
public:
std::vector<std::vector<std::string> > groupAnagrams(std::vector<std::string>& strs) {
Copy link

Choose a reason for hiding this comment

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

C++11 以降 >> のあいだにスペースを空けなくて済むようになっています。スペースを消すことをおすすめします。

https://cpprefjp.github.io/lang/cpp11/right_angle_brackets.html

C++11からは、 vector<basic_string> のように、2つ以上連続する右山カッコの間に、スペースを入力する必要がなくなった。

Copy link
Owner Author

Choose a reason for hiding this comment

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

@nodchip
コメントありがとうございます。
こちら確認不足でした。一読しておきます。

public:
std::vector<std::vector<std::string> > groupAnagrams(std::vector<std::string>& strs) {
std::unordered_map<std::string, std::vector<std::string> > groups;
for (const std::string& s : strs) {
Copy link

Choose a reason for hiding this comment

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

for (const auto& s : strs) {

のほうがシンプルだと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

@nodchip
ご指摘ありがとうございます。
こちらの方が明示的かと思いましたが、これくらいであれば

for (const auto& s : strs) {

の方が読みやすいかもしれないですね。
ありがとうございます。

}

std::vector<std::vector<std::string> > result;
result.reserve(groups.size());
Copy link

Choose a reason for hiding this comment

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

reserve() をすることにより、あらかじめ必要な要素数分のメモリを確保し、 push_back() 中のメモリの再確保を避けることができるのですが、その効果が十分かどうかは計測してみないと何とも言えません。処理時間に対する要求を考慮し、シビアでない場合、 コードをシンプルにするため、reserve() を呼び出さないほうがよい場合もあると思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

@nodchip
ありがとうございます。
効果の測定まで検証できておりませんでした。chronoで時間を測ってみたいと思います。

@5103246
Copy link

5103246 commented Nov 23, 2025

全体的に読みやすいと思います。

@5ky7
Copy link

5ky7 commented Dec 12, 2025

全体として読みやすかったです.


class Solution {
public:
std::vector<std::vector<std::string> > groupAnagrams(std::vector<std::string>& strs) {
Copy link

Choose a reason for hiding this comment

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

非const参照で受け取ると変更の可能性があることを呼び出し側は考慮することになるので、内部で変更をしないのであればconstをつけてもよいかと思います。

for (const std::string& word : strs) {
std::string key = word;
std::sort(key.begin(), key.end());
groups[key].push_back(s);
Copy link

Choose a reason for hiding this comment

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

sではなくwordでしょうか。

class Solution {
public:
std::vector<std::vector<std::string> > groupAnagrams(std::vector<std::string>& strs) {
std::unordered_map<std::string, std::vector<std::string> > groups;
Copy link

Choose a reason for hiding this comment

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

なにをキーにしてグループをまとめているかの情報が後を読まないとわからないので、sorted_str_to_groups(sorted_str_to_anagramsなど)くらいでもよいかなと思いました。

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.

5 participants