-
Notifications
You must be signed in to change notification settings - Fork 0
49. Group Anagrams.md #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
X-XsleepZzz
wants to merge
4
commits into
main
Choose a base branch
from
X-XsleepZzz-patch-2
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
Show all changes
4 commits
Select commit
Hold shift + click to select a range
93d89ea
Add problem description for Group Anagrams
X-XsleepZzz 59ac014
Refactor Group Anagrams problem statement and solution
X-XsleepZzz e59fda2
Fix formatting in Group Anagrams constraints
X-XsleepZzz 9679078
Fix formatting and add step1 section in markdown
X-XsleepZzz 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,93 @@ | ||
| 49. Group Anagrams | ||
| Given an array of strings strs, group the anagrams together. You can return the answer in any order. | ||
|
|
||
| Example 1: | ||
|
|
||
| Input: strs = ["eat","tea","tan","ate","nat","bat"] | ||
|
|
||
| Output: [["bat"],["nat","tan"],["ate","eat","tea"]] | ||
|
|
||
| Explanation: | ||
|
|
||
| There is no string in strs that can be rearranged to form "bat". | ||
| The strings "nat" and "tan" are anagrams as they can be rearranged to form each other. | ||
| The strings "ate", "eat", and "tea" are anagrams as they can be rearranged to form each other. | ||
| Example 2: | ||
|
|
||
| Input: strs = [""] | ||
|
|
||
| Output: [[""]] | ||
|
|
||
| Example 3: | ||
|
|
||
| Input: strs = ["a"] | ||
|
|
||
| Output: [["a"]] | ||
|
|
||
| Constraints: | ||
| 1 <= strs.length <= 10^4 | ||
| 0 <= strs[i].length <= 100 | ||
| strs[i] consists of lowercase English letters. | ||
| (https://leetcode.com/problems/group-anagrams/description/) | ||
|
|
||
| --- | ||
| step1 | ||
| ```py | ||
| class Solution: | ||
| def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
| key_to_char = {} | ||
|
|
||
| for char in strs: | ||
| key = "".join(sorted(char)) | ||
|
|
||
| if key not in key_to_char: | ||
| key_to_char[key] = [] | ||
| key_to_char[key].append(char) | ||
| return list(key_to_char.values()) | ||
| ``` | ||
| strs.lengthをnとして、strs[i].length、つまり文字列の長さをkとする。 | ||
| 時間計算量: for文をstrs.length回、回すのでO(n)、中でsortedするのにO(klogk)、joinで文字列に整えて出すのにO(k)、つまり全体でO(n*klogk) | ||
| 空間計算量: 辞書でkeyとしてsortした文字列を入れているのでO(k)、valueにはもとの文字列を入れるのでO(k)、よって内部ではO(k)。for文をstrs.length回、回すのでO(k*n) | ||
|
|
||
| 時間計算量は、最悪10^4 * 2*10^2 * log10 で大体10^6 < 10^7なのでセーフ | ||
| key_to_charって名前で良いのか少し気になる。 | ||
|
|
||
| --- | ||
| (https://github.com/komdoroid/arai60/tree/14ec1184d68b0514455e688bdfc2ef39e1d787d8/HashMap/49.GroupAnagrams) | ||
| を読んだ。 | ||
|
|
||
| defaultdictだとキーがないときに、勝手に穴埋めしてくれるので、こっちを使ったほうが良さそう。 | ||
| リストを引数に入れたら、キーがないとき[]をvalueに入れてくれる。 | ||
| 辞書の変数名はかなり長くなるけど、"sorted_str_to_words"にする。 | ||
|
|
||
| step2 | ||
| ```py | ||
| import collections | ||
| class Solution: | ||
| def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
| sorted_str_to_words = collections.defaultdict(list) | ||
|
|
||
| for original_str in strs: | ||
| sorted_str = "".join(sorted(original_str)) | ||
| sorted_str_to_words[sorted_str].append(original_str) | ||
| return list(sorted_str_to_words.values()) | ||
| ``` | ||
|
|
||
| 変数名を少し考えてつけてみたけど、長すぎて逆に可読性落ちていないか心配。 | ||
| また、タイポしそう。 | ||
|
|
||
| step3 3回連続pass | ||
| ```py | ||
| import collections | ||
| class Solution: | ||
| def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
| sorted_str_to_words = collections.defaultdict(list) | ||
|
|
||
| for original_str in strs: | ||
| sorted_str = "".join(sorted(original_str)) | ||
| sorted_str_to_words[sorted_str].append(original_str) | ||
| return list(sorted_str_to_words.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. その感覚大事ですね。 |
||
|
|
||
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.
step2以降では修正されていますが、
charだとミスリーディングですね。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.
ご指摘ありがとうございますmm
たしかにそうですね。。この時、charの意味を誤解して書いていました