-
Notifications
You must be signed in to change notification settings - Fork 0
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
Open
tom4649
wants to merge
1
commit into
main
Choose a base branch
from
49.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.
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,11 @@ | ||
| # 49. Group Anagrams | ||
|
|
||
| - [リンク](https://leetcode.com/problems/group-anagrams/description/) | ||
| - 愚直にsol1を書いたが計算量はO(nllog(l)) | ||
| - histogramを用いる解法 | ||
| - https://github.com/ichika0615/arai60/pull/11/changes#diff-fa3129c54f54ceaf0d237e449d3491c5eb44eaa4660ae11dec87df81d245cb25 | ||
| - リストはmutableでhashableではないのでtupleに変換する必要がある | ||
| - 計算量はO(nl) | ||
| - 入力がアルファベット小文字以外のときには例外処理で対応 | ||
| - 実行時間はsol1の方が速い | ||
| - pythonのsortedが高速でloglが定数倍より速い? | ||
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]]: | ||
| anagram_sets = defaultdict(list) | ||
| for orig_str in strs: | ||
| sorted_str = sorted(orig_str) | ||
|
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. 私は "".join まで上の行でしてしまうかもしれません。まあ、どちらでもいいです。
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. sorted_key = "".join(sorted(orig_str))
anagram_sets[sorted_key].append(orig_str)この方が見やすいですね |
||
| anagram_sets["".join(sorted_str)].append(orig_str) | ||
| return list(anagram_sets.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,22 @@ | ||
| from collections import defaultdict | ||
|
|
||
| NUM_POSSIBLE_CHARS = 26 | ||
| START_CHAR = "a" | ||
| ORD_START_CHAR = ord(START_CHAR) | ||
|
|
||
|
|
||
| class Solution: | ||
| def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
| def str_to_frequency(input_str): | ||
| frequency = [0] * NUM_POSSIBLE_CHARS | ||
| for input_char in input_str: | ||
| ord_input_char_rel = ord(input_char) - ORD_START_CHAR | ||
| if ord_input_char_rel >= NUM_POSSIBLE_CHARS: | ||
| raise ValueError | ||
| frequency[ord_input_char_rel] += 1 | ||
| return tuple(frequency) | ||
|
|
||
| frequency_to_strs = defaultdict(list) | ||
| for input_str in strs: | ||
| frequency_to_strs[str_to_frequency(input_str)].append(input_str) | ||
| return list(frequency_to_strs.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.
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.
Native code の部分だというだけで感覚10-20倍程度速いです。
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.
なるほど、そう考えると色々な組み込み関数は知っておくと良さそうです。(基数ソートをRadix sortというの知らなかったです。これが一番速いんですね)