Skip to content

Conversation

@syoshida20
Copy link
Owner

@syoshida20 syoshida20 commented May 5, 2025

問題URL

問題文

Given an array of strings strs, group the anagrams together. You can return the answer in any order.

Example 1:

  • Input: strs = ["eat","tea","tan","ate","nat","bat"]
  • Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
  • Explanation:
    There is no string in strs that can be rearranged to form "bat".
    The strings "nat" and "tan" are anagrams as they can be rearranged to form each other.
    The strings "ate", "eat", and "tea" are anagrams as they can be rearranged to form each other.

Example 2:

  • Input: strs = [""]
  • Output: [[""]]

Example 3:

  • Input: strs = ["a"]
  • Output: [["a"]]

Constraints:

  • 1 <= strs.length <= 104
  • 0 <= strs[i].length <= 100
  • strs[i] consists of lowercase English letters.

@syoshida20 syoshida20 marked this pull request as ready for review May 6, 2025 07:51
```javascript
const groupAnagrams = function(strs) {
const sorted_to_anagrams = new Map()
for (const original_word of strs) {

Choose a reason for hiding this comment

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

original_word, soretd_wordは名前としてごちゃっとしすぎかなと感じます。
変数名が長いので、パッと見た時にロジックを追うよりも、変数名の識別に脳の容量が割かれる感じです。
original_word $\rightarrow$ s or word
soretd_word $\rightarrow$ sorted or t
とかが好きです。

コメント集のこの部分が参考になると思います。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.fcs3httrll4l

Copy link
Owner Author

@syoshida20 syoshida20 May 10, 2025

Choose a reason for hiding this comment

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

ありがとうございます。

パッと見た時にロジックを追うよりも、変数名の識別に脳の容量が割かれる感じです。

こちらの感覚を僕は持てていませんでした。参考になります。

コメント集を見て、短くても正しく伝わる方法を模索しようと思います。

original_words.push(original_word)
sorted_to_anagrams.set(sorted_word, original_words)
}
const ans = new Array()
Copy link

@chanseok-lim chanseok-lim May 6, 2025

Choose a reason for hiding this comment

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

もしpythonだったらconst ans = Array()return ansの直前で改行を入れたくなります。javaにおける改行ってどんな感じなんでしょうか?

また ansという変数名は「問題への答え」という気持ちが前に出過ぎていて、システムに組み込み込まれているとしたら変だなという感じです。

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 ans = Array()の前の改行に関しては、どちらものケースもあるかなと思います。
return前に改行を入れるのはあまり見ないイメージがあります。

変数名についてもシステムに組み込むことを意識し、resultという変数を使おうと思います!

Copy link

Choose a reason for hiding this comment

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

改行の入れ方はわりと自由です。慣れるにつれてだんだん改行が減ってくる傾向があるかなとは思います。


### 他の人のPRを読んで

* Pythonのdefaultdictのkeyについて、何を入力としてOKかを調べようとしたが、ドキュメントを探しても見つからず調べ方を教えて欲しいです。
Copy link

@chanseok-lim chanseok-lim May 6, 2025

Choose a reason for hiding this comment

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

defaultdictのkeyもdictのkeyと同じです。

default は組み込みの dict クラスのサブクラスです。これはメソッドをひとつオーバーライドし、書き込み可能なインスタンス変数をひとつ追加しています。それ以外の機能は dict クラスと同じです
https://docs.python.org/ja/3.13/library/collections.html#collections.defaultdict:~:text=defaultdict%20%E3%81%AF%E7...

辞書のキーには、 ほぼ どんな値も使うことができます。 キーとして使えないのは、 hashable (ハッシュ可能) でない値、すなわちリストや辞書のようなミュータブルな型 (内包する値ではなくオブジェクト自体が同一であるかによって比較が行われるような型)です。https://docs.python.org/ja/3/library/stdtypes.html#dict:~:text=%E8%BE%9E%E6%9B...

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます!

tupleが辞書のキーにできると言っていた箇所が原文を確認し、理解できました。

}
const original_words = sorted_to_anagrams.get(sorted_word)
original_words.push(original_word)
sorted_to_anagrams.set(sorted_word, original_words)
Copy link

Choose a reason for hiding this comment

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

JavaScript 知らないんですが、この set っているんですか?

Copy link
Owner Author

@syoshida20 syoshida20 May 10, 2025

Choose a reason for hiding this comment

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

質問の意図を汲み取れているのか怪しいので、質問をさせてください。

この set っているんですか?

こちらは、どういう意味でしょうか?

Copy link

Choose a reason for hiding this comment

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

こういうことです。

const m = new Map();
m.set(1, []);
const a = m.get(1);
a.push("hello");
console.log(m.get(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.

意図が理解できました!

コードを試したところ、mapのvalueが正しく置き換わっていることを確認しました。

> const m = new Map();
> m.set(1, []);
> const a = m.get(1);
> a.push("hello");
> console.log(m.get(1));
[ 'hello' ]

また、Mapのget関数のドキュメントを参照したところ、MapのValueがObjectの場合には、更新可能という記載を見つけました。 調べる習慣/量が足りていないので、増やそうと思います。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get

If the value that is associated to the provided key is an object, then you will get a reference to that object and any change made to that object will effectively modify it inside the Map object.

Copy link
Owner Author

@syoshida20 syoshida20 May 11, 2025

Choose a reason for hiding this comment

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

ObjectにArrayが含まれていることも確認しました!

Objectの一覧: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
Primitive Value (Objectでないデータ型)の一覧 : https://developer.mozilla.org/en-US/docs/Glossary/Primitive

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants