Conversation
| def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
| result: List[List[int]] = [] | ||
| sorted_str_idx: Dict[str, int] = {} | ||
| for str_i in strs: |
There was a problem hiding this comment.
strが使えないために咄嗟にiをつけたものだと思いますが、str_iという変数名はあまり見ない気がします。iが意味を持ててないですし、ともすればindexであるという誤解を招くこともあろうかと思います。
|
|
||
| - Dictのvaluesに直接文字列を入れて、values()を出力する方法もある | ||
| - コードとしてもこちらの方が最小限で書けている | ||
| - ただ、この状態だとkeyが`"['a', 'e', 't']"` のようになってしまい少し違和感がある |
|
|
||
| ``` | ||
|
|
||
| - 異常な入力に関して、空リストが来たら空リストを返す |
There was a problem hiding this comment.
空リスト以外の異常な入力にはどのようなものがあり得て、どう対処すればよいでしょうか?
There was a problem hiding this comment.
リストじゃないとか、リストに文字列以外のものが入っているとかですね。
リストでないiterableはfor文回せてしまうのでその前でエラー出したいですね。
リストに文字列以外が来た場合はこれもsort出来てしまわないようにエラーだしたいですね。
具体的にこういった場合を考慮したコードも書いていこうと思います。ご指摘ありがとうございます
There was a problem hiding this comment.
リストじゃないとか、リストに文字列以外のものが入っているとかですね。
そうですね。
リストでない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) |
There was a problem hiding this comment.
1行に3回もanagramという文字が並んでいて辛いです。もう少し視認性を良くできませんか?
There was a problem hiding this comment.
迷彩みたいな話ですね。変数名は長ければよいというものではないです。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.fcs3httrll4l
There was a problem hiding this comment.
気を付けたいと思います。anagramをわざわざ入れなくても良いところでは避けようと思います
|
|
||
| ### Complexity Analysis | ||
|
|
||
| - 時間計算量:O(n * m log m) |
There was a problem hiding this comment.
文字列長は≤100で文字列数は≤10000なので、10^6ぐらいのオーダーになりそうです。10^-2秒のオーダーで計算できるのかなと思います。
確かにこういった時間の正確な見積もりはあまりイメージを持って出来ていなかったので参考になります。
There was a problem hiding this comment.
文字列のソートは 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 秒くらいになると思います。
There was a problem hiding this comment.
試してみるといいかもしれませんね。
文字列のソートはネイティブコードで走るので、もう少し速いかもしれません。
| grouped_anagrams: Dict[str, List[str]] = defaultdict(list) | ||
| for anagram in strs: | ||
| sorted_anagram = "".join(sorted(anagram)) | ||
| grouped_anagrams[sorted_anagram].append(anagram) |
There was a problem hiding this comment.
迷彩みたいな話ですね。変数名は長ければよいというものではないです。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.fcs3httrll4l
|
|
||
| - 異常な入力に関して、空リストが来たら空リストを返す | ||
| - 空文字列が来ていないのに空文字列を返すのもおかしいのでこれで良さそう | ||
| - tupleとfrozensetはimmutableだが、要素がhashableな場合だけhashableとのこと |
There was a problem hiding this comment.
| class Solution: | ||
| def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
| result: List[List[int]] = [] | ||
| sorted_str_idx: Dict[str, int] = {} |
There was a problem hiding this comment.
sorted_str_idx という変数名ですと、ソートされた文字列のインデックスという意味に感じられます。また、英単語から文字を削って変数名にすると、やや認知負荷が上がるように思います。 to を入れて、 sorted_to_str_index はいかがでしょうか?
There was a problem hiding this comment.
少し冗長かなと感じてしまいましたが、その方が分かりやすいですね
|
|
||
| ### Complexity Analysis | ||
|
|
||
| - 時間計算量:O(n * m log m) |
There was a problem hiding this comment.
文字列のソートは 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 秒くらいになると思います。
問題文:https://leetcode.com/problems/group-anagrams/description/