Skip to content

Solved Arai60/49. Group Anagrams#12

Open
Satorien wants to merge 1 commit intomainfrom
solve/arai60_49
Open

Solved Arai60/49. Group Anagrams#12
Satorien wants to merge 1 commit intomainfrom
solve/arai60_49

Conversation

@Satorien
Copy link
Copy Markdown
Owner

@Satorien Satorien commented May 1, 2025

def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
result: List[List[int]] = []
sorted_str_idx: Dict[str, int] = {}
for str_i in strs:
Copy link
Copy Markdown

@chanseok-lim chanseok-lim May 1, 2025

Choose a reason for hiding this comment

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

strが使えないために咄嗟にiをつけたものだと思いますが、str_iという変数名はあまり見ない気がします。iが意味を持ててないですし、ともすればindexであるという誤解を招くこともあろうかと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

素直にstringとかで良かったんでしょうか?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

sでいいと思います。


- Dictのvaluesに直接文字列を入れて、values()を出力する方法もある
- コードとしてもこちらの方が最小限で書けている
- ただ、この状態だとkeyが`"['a', 'e', 't']"` のようになってしまい少し違和感がある
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

はい。私もそのkeyには違和感があります。


```

- 異常な入力に関して、空リストが来たら空リストを返す
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

空リスト以外の異常な入力にはどのようなものがあり得て、どう対処すればよいでしょうか?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

リストじゃないとか、リストに文字列以外のものが入っているとかですね。
リストでないiterableはfor文回せてしまうのでその前でエラー出したいですね。
リストに文字列以外が来た場合はこれもsort出来てしまわないようにエラーだしたいですね。
具体的にこういった場合を考慮したコードも書いていこうと思います。ご指摘ありがとうございます

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

リストじゃないとか、リストに文字列以外のものが入っているとかですね。

そうですね。

リストでないiterableはfor文回せてしまうのでその前でエラー出したいですね。

組み込みのisinstanceを使うのがいいと思います。
(https://docs.python.org/ja/3.13/library/functions.html#isinstance)[https://docs.python.org/ja/3.13/library/functions.html#isinstance]

リストに文字列以外が来た場合はこれもsort出来てしまわないようにエラーだしたい

そうですね。あとはlowercase / uppercaeはどう扱うとかも考えられそうです。
例えば"aa"と"aA"みたいなケースです。

grouped_anagrams: Dict[str, List[str]] = defaultdict(list)
for anagram in strs:
sorted_anagram = "".join(sorted(anagram))
grouped_anagrams[sorted_anagram].append(anagram)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

1行に3回もanagramという文字が並んでいて辛いです。もう少し視認性を良くできませんか?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

迷彩みたいな話ですね。変数名は長ければよいというものではないです。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.fcs3httrll4l

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

気を付けたいと思います。anagramをわざわざ入れなくても良いところでは避けようと思います


### Complexity Analysis

- 時間計算量:O(n * m log m)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

今回の範囲だとこの処理は何秒くらいかかりそうでしょうか?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

文字列長は≤100で文字列数は≤10000なので、10^6ぐらいのオーダーになりそうです。10^-2秒のオーダーで計算できるのかなと思います。
確かにこういった時間の正確な見積もりはあまりイメージを持って出来ていなかったので参考になります。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

文字列のソートは O(m log m) ですので、 100 * 7 くらいになると思います。
また、 Python はかなり遅い言語で、 1 秒間に 100~1000 万ステップくらいしか実行できないと思います。
https://github.com/niklas-heer/speed-comparison
https://benchmarksgame-team.pages.debian.net/benchmarksgame/box-plot-summary-charts.html

諸々を考えると 1 秒くらいになると思います。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

試してみるといいかもしれませんね。
文字列のソートはネイティブコードで走るので、もう少し速いかもしれません。

grouped_anagrams: Dict[str, List[str]] = defaultdict(list)
for anagram in strs:
sorted_anagram = "".join(sorted(anagram))
grouped_anagrams[sorted_anagram].append(anagram)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

迷彩みたいな話ですね。変数名は長ければよいというものではないです。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.fcs3httrll4l


- 異常な入力に関して、空リストが来たら空リストを返す
- 空文字列が来ていないのに空文字列を返すのもおかしいのでこれで良さそう
- tupleとfrozensetはimmutableだが、要素がhashableな場合だけhashableとのこと
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

こういうのは、公式サイトへのリンクを貼りましょう。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.9xjrags8izok

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

すみません、承知しました。

class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
result: List[List[int]] = []
sorted_str_idx: Dict[str, int] = {}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

sorted_str_idx という変数名ですと、ソートされた文字列のインデックスという意味に感じられます。また、英単語から文字を削って変数名にすると、やや認知負荷が上がるように思います。 to を入れて、 sorted_to_str_index はいかがでしょうか?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

少し冗長かなと感じてしまいましたが、その方が分かりやすいですね


### Complexity Analysis

- 時間計算量:O(n * m log m)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

文字列のソートは O(m log m) ですので、 100 * 7 くらいになると思います。
また、 Python はかなり遅い言語で、 1 秒間に 100~1000 万ステップくらいしか実行できないと思います。
https://github.com/niklas-heer/speed-comparison
https://benchmarksgame-team.pages.debian.net/benchmarksgame/box-plot-summary-charts.html

諸々を考えると 1 秒くらいになると思います。

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.

4 participants