-
Notifications
You must be signed in to change notification settings - Fork 0
Solved Arai60/49. Group Anagrams #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| ## Step 1. Initial Solution | ||
|
|
||
| - 文字列をそれぞれソートして一致するものを同じグループに入れる | ||
| - 辞書型にしてソートされた文字列と結果のインデックスを紐づけておく | ||
| - 文字列に対してsortedを使うとどうなるのか | ||
| - sortされた文字列が返ってくる前提でやるとエラー | ||
| - リストが返ってきていそうなのでstr()で文字列化 | ||
|
|
||
| ```python | ||
| class Solution: | ||
| 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. strが使えないために咄嗟にiをつけたものだと思いますが、
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 素直にstringとかで良かったんでしょうか? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| sorted_str = str(sorted(str_i)) | ||
| if sorted_str not in sorted_str_idx: | ||
| sorted_str_idx[sorted_str] = len(result) | ||
| result.append([str_i]) | ||
| continue | ||
| idx = sorted_str_idx[sorted_str] | ||
| result[idx].append(str_i) | ||
| return result | ||
| ``` | ||
|
|
||
| ### Complexity Analysis | ||
|
|
||
| - 時間計算量:O(n * m log m) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 今回の範囲だとこの処理は何秒くらいかかりそうでしょうか?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 文字列長は≤100で文字列数は≤10000なので、10^6ぐらいのオーダーになりそうです。10^-2秒のオーダーで計算できるのかなと思います。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 文字列のソートは O(m log m) ですので、 100 * 7 くらいになると思います。 諸々を考えると 1 秒くらいになると思います。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 試してみるといいかもしれませんね。 |
||
| - 文字列の数×文字列のソート | ||
| - 文字列化の実装の中身は詳しく分かっていない | ||
| - __str__メソッドの定義による | ||
| - print()で出力されるものと同じ | ||
| - 空間計算量:O(n) | ||
| - 文字列の数 | ||
|
|
||
| ## Step 2. Alternatives | ||
|
|
||
| - Dictのvaluesに直接文字列を入れて、values()を出力する方法もある | ||
| - コードとしてもこちらの方が最小限で書けている | ||
| - ただ、この状態だとkeyが`"['a', 'e', 't']"` のようになってしまい少し違和感がある | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. はい。私もそのkeyには違和感があります。 |
||
|
|
||
| ```python | ||
| class Solution: | ||
| def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
| sorted_to_group: Dict[str, List[str]] = defaultdict(list) | ||
| for anagram in strs: | ||
| sorted_to_group[str(sorted(anagram))].append(anagram) | ||
| return list(sorted_to_group.values()) | ||
| ``` | ||
|
|
||
| - str()を呼び出すよりも””.join()を使う方が良い | ||
| - strでは各要素のあとにカンマを入れる処理が必要なので遅い | ||
| - keyも`"aet"` のようになって分かりやすい | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
| sorted_to_group: Dict[str, List[str]] = defaultdict(list) | ||
| for anagram in strs: | ||
| sorted_to_group["".join(sorted(anagram))].append(anagram) | ||
| return list(sorted_to_group.values()) | ||
| ``` | ||
|
|
||
| - あまり使う意味はないかもしれないが、tupleにするという手もあると思った | ||
| - keyは`["a","e","t"]` のようになる | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
| sorted_to_group: Dict[Tuple[str], List[str]] = defaultdict(list) | ||
| for anagram in strs: | ||
| sorted_in_tuple: Tuple[str] = tuple(sorted(anagram)) | ||
| sorted_to_group[sorted_in_tuple].append(anagram) | ||
| return list(sorted_to_group.values()) | ||
|
|
||
| ``` | ||
|
|
||
| - 異常な入力に関して、空リストが来たら空リストを返す | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 空リスト以外の異常な入力にはどのようなものがあり得て、どう対処すればよいでしょうか?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. リストじゃないとか、リストに文字列以外のものが入っているとかですね。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
そうですね。
組み込みのisinstanceを使うのがいいと思います。
そうですね。あとはlowercase / uppercaeはどう扱うとかも考えられそうです。 |
||
| - 空文字列が来ていないのに空文字列を返すのもおかしいのでこれで良さそう | ||
| - tupleとfrozensetはimmutableだが、要素がhashableな場合だけhashableとのこと | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. すみません、承知しました。 |
||
| - 脳死で辞書型のkeyに使えると思わないようにする | ||
|
|
||
| ## Step 3. Final Solution | ||
|
|
||
| - 命名はこの辺りが分かりやすそうに感じた | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
| 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1行に3回もanagramという文字が並んでいて辛いです。もう少し視認性を良くできませんか? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 迷彩みたいな話ですね。変数名は長ければよいというものではないです。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 気を付けたいと思います。anagramをわざわざ入れなくても良いところでは避けようと思います |
||
| return list(grouped_anagrams.values()) | ||
|
|
||
| ``` | ||
There was a problem hiding this comment.
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 はいかがでしょうか?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
少し冗長かなと感じてしまいましたが、その方が分かりやすいですね