-
Notifications
You must be signed in to change notification settings - Fork 0
Group Anagrams #26
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?
Group Anagrams #26
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,20 @@ | ||
| """ | ||
| 文字列の長さは重要 | ||
| 100 | ||
| これだと、順列を求めてとかはキビシイ | ||
| 要は、出現回数が同じなら良い | ||
| もしくは、ソートした結果が同じでもよい | ||
| これがよさそう | ||
|
|
||
| 5m | ||
|
|
||
| sorted(s)で返るのはlistじゃないことに注意 | ||
| .values()で返るのもlistじゃなくてdict_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. ドキュメントにも目を通すと良いと思います。
|
||
| """ | ||
| from collections import defaultdict | ||
| class Solution: | ||
| def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
| sorted_str_to_strs = defaultdict(list) | ||
|
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. まあいいんじゃないでしょうか。 |
||
| for s in strs: | ||
| sorted_str_to_strs["".join(sorted(s))].append(s) | ||
| return sorted_str_to_strs.values() | ||
| 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()) |
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.
ドキュメントにも目を通すと良いと思います。
https://docs.python.org/3/library/functions.html#sorted
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.
ありがとうございます。
目を通しておきます👀