Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions arai60/group-anagrams/step1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""
文字列の長さは重要
100
これだと、順列を求めてとかはキビシイ
要は、出現回数が同じなら良い
もしくは、ソートした結果が同じでもよい
これがよさそう

5m

sorted(s)で返るのはlistじゃないことに注意
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ドキュメントにも目を通すと良いと思います。

Return a new sorted list from the items in iterable.

https://docs.python.org/3/library/functions.html#sorted

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.

ありがとうございます。
目を通しておきます👀

.values()で返るのもlistじゃなくてdict_valuesという定義されたオブジェクト?
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ドキュメントにも目を通すと良いと思います。

Return a new view of the dictionary’s values.

https://docs.python.org/3/library/stdtypes.html#dict.values

"""
from collections import defaultdict
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
sorted_str_to_strs = defaultdict(list)
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_to_originalとかはどうでしょうか?

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.

これ微妙ですよね~
ただ、個人的にはsorted_str_to_originalもあまりピンとこないんですよね... (私がoriginalに意味を見出しすぎているのかもしれないです)
sorted_str_to_anagramsだとどうなんでしょう?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

まあいいんじゃないでしょうか。

for s in strs:
sorted_str_to_strs["".join(sorted(s))].append(s)
return sorted_str_to_strs.values()
32 changes: 32 additions & 0 deletions arai60/group-anagrams/step2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
https://leetcode.com/problems/group-anagrams/solutions/3687735/beats-100-c-java-python-beginner-friendly/?envType=problem-list-v2&envId=me1nua2e
-> anagram_map なるほど
https://github.com/SuperHotDogCat/coding-interview/pull/13/files
-> sorted_str_to_anagramsのほうがよさそうだな
https://github.com/t-ooka/leetcode/pull/2/files
-> 出現回数を持つやり方。26個のタプルを入れるのに、listからtupleに変換すればよいのはたしかに

sorted_str_to_strs.values() -> list(sorted_str_to_strs.values())
sorted_str_to_strs -> sorted_str_to_anagrams
"""

from collections import defaultdict
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
sorted_str_to_anagrams = defaultdict(list)
for s in strs:
sorted_str_to_anagrams["".join(sorted(s))].append(s)
return list(sorted_str_to_anagrams.values())

# 出現回数を持つ
from collections import defaultdict
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
NUM_ALPHABET = 26
sorted_str_to_anagrams = defaultdict(list)
for s in strs:
char_count = [0] * NUM_ALPHABET
for c in s:
char_count[ord(c) - ord('a')] += 1
sorted_str_to_anagrams[tuple(char_count)].append(s)
return list(sorted_str_to_anagrams.values())