-
Notifications
You must be signed in to change notification settings - Fork 0
49. Group Anagrams #13
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
Open
nanae772
wants to merge
1
commit into
main
Choose a base branch
from
049-group-anagrams
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+82
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# ステップ1 | ||
|
||
文字列の集まりが与えられるので、アナグラム(文字の入れ替え)で同じになるものをまとめるというタスク。 | ||
|
||
文字列がアナグラムで一致するかどうかは文字列の各文字をソートしたものが一致するかどうかを見ればよい。 | ||
なのでソートされた文字列をkeyとして、そのvalueを元の文字列の配列にすれば効率的にまとめられる。 | ||
今回はdefaultdictを使うと便利そう | ||
https://docs.python.org/3/library/collections.html#collections.defaultdict | ||
|
||
とりあえず書けた。 | ||
|
||
# ステップ2 | ||
|
||
他の解き方は無いか考えて、UnionFindでも解けるかなと思った。 | ||
ただUnionFindクラスを実装しなければならずUnionFindを使うメリットもそこまで無さそうなので止めておく。 | ||
|
||
問題自体はすぐ終わったので「エンジニアリングをする」という観点で、少しこのクラスを使う側の視点で考えてみる。 | ||
この関数も返り値の順序が割と適当だが、順序を決めるとしたらどういう順序だと使う側は嬉しいだろうか。 | ||
各グループの中身はシンプルにアルファベットの昇順がよさそう。 | ||
グループ間の順序は…要素数順が適切だろうか。 | ||
「この文字列の集まりをアナグラムでグルーピングしておいて」 | ||
といったときにどういうことを期待するんだろうか。 | ||
あまりユースケースが思いつかないが、何となくグループされた文字列が多いものを知りたいような気もするので | ||
グループ間の順序をつけるとしたら要素数の降順? | ||
でも降順はなんか不自然な気もするからやはり昇順が自然? | ||
うーん、分からない。 | ||
|
||
有用性や一意性のためにソートするのは実行時間がかかることとのトレードオフにもなりそう。 | ||
要素数順のソートはそんなにかからないだろうが、各グループ間の文字列をアルファベット順にソートするには | ||
全て1つのグループに入る場合の最悪を考えるとO(NMlogN)か。(N:文字列の数、M:1つの文字列の長さの最大) | ||
|
||
他の人の解答も少し見る。 | ||
|
||
https://github.com/akmhmgc/arai60/pull/9/files | ||
|
||
「各文字列が英小文字である」という条件が崩れた場合にどうなるか、というのも確かに考えておいたほうがいいのか。 | ||
step1の解法では文字列をソートするので多分問題は無さそうだが、 | ||
英小文字が26文字であることを利用して各文字をカウントしてカウント数で比較する | ||
という手法を取っている場合は前提条件が崩れるとうまく動かなくなるということが起きる。 | ||
|
||
また26文字であることを利用した解法も考えてはいた。確かに計算量上はソートより優位であるけれど、 | ||
Pythonで文字列を1つずつ見ていってカウントして、という処理をするより、 | ||
Cで実装されている組み込みのsortを使ったほうがおそらく速いだろうという感覚だった。 | ||
|
||
https://github.com/h1rosaka/arai60/pull/16/files | ||
|
||
defaultdictを実装されている方もいた。 | ||
勉強になりそうなので時間があるときにやってみてもいいかもしれない。 | ||
|
||
# ステップ3 | ||
|
||
3回連続で通せるようになったので一旦完了。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from collections import defaultdict | ||
|
||
|
||
class Solution: | ||
def groupAnagrams(self, strs: list[str]) -> list[list[str]]: | ||
group_anagrams: dict[str, list[str]] = defaultdict(list) | ||
nanae772 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for s in strs: | ||
nanae772 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sorted_string = "".join(sorted(s)) | ||
group_anagrams[sorted_string].append(s) | ||
return list(group_anagrams.values()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from collections import defaultdict | ||
|
||
|
||
class Solution: | ||
def groupAnagrams(self, strs: list[str]) -> list[list[str]]: | ||
sorted_to_originals: dict[str, list[str]] = defaultdict(list) | ||
for s in strs: | ||
sorted_string = "".join(sorted(s)) | ||
sorted_to_originals[sorted_string].append(s) | ||
return list(sorted_to_originals.values()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from collections import defaultdict | ||
|
||
|
||
class Solution: | ||
def groupAnagrams(self, strs: list[str]) -> list[list[str]]: | ||
sorted_to_originals = defaultdict(list) | ||
for s in strs: | ||
sorted_string = "".join(sorted(s)) | ||
nanae772 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sorted_to_originals[sorted_string].append(s) | ||
return list(sorted_to_originals.values()) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.