-
Notifications
You must be signed in to change notification settings - Fork 0
Group Anagrams.md #11
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
katataku
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,79 @@ | ||
URL: https://leetcode.com/problems/group-anagrams/description/ | ||
|
||
# Step 1 | ||
|
||
- 実装時間: 4分 | ||
- 入力文字列の個数をn、最大の文字列長をmとして | ||
- 時間計算量: O(n * mlogm)。文字列のソートをn回するので。辞書への登録/参照はO(1). | ||
- 空間計算量: O(n) | ||
|
||
```python | ||
class Solution: | ||
def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
sorted_str_to_strs = defaultdict(list) | ||
for s in strs: | ||
sorted_string = str(sorted(s)) | ||
sorted_str_to_strs[sorted_string].append(s) | ||
return list(sorted_str_to_strs.values()) | ||
``` | ||
|
||
- `str`や`string`は既存の処理があるので、避けた。スコープも短いし、変数名を`s`にした。 | ||
|
||
# Step 2 | ||
|
||
- 参考にしたURL | ||
- https://github.com/Exzrgs/LeetCode/pull/26 | ||
- https://github.com/fhiyo/leetcode/pull/15 | ||
- https://github.com/nittoco/leetcode/pull/13 | ||
- https://github.com/hayashi-ay/leetcode/pull/19 | ||
- https://github.com/TORUS0818/leetcode/pull/14 | ||
- https://github.com/Ryotaro25/leetcode_first60/pull/13 | ||
- https://github.com/Mike0121/LeetCode/pull/29 | ||
- https://github.com/kazukiii/leetcode/pull/13 | ||
- https://github.com/Yoshiki-Iwasa/Arai60/pull/11 | ||
- https://github.com/rihib/leetcode/pull/3 | ||
- https://github.com/seal-azarashi/leetcode/pull/12 | ||
- https://github.com/hroc135/leetcode/pull/12 | ||
- https://github.com/colorbox/leetcode/pull/26 | ||
- https://github.com/tarinaihitori/leetcode/pull/12 | ||
|
||
- .values()の戻り値がview of the dictionary’s valuesということを知らなかったので確認 | ||
- https://docs.python.org/3/library/stdtypes.html#dict.values | ||
|
||
- `sorted_str_to_strs`はイマイチ。 | ||
- `sorted_str_to_original`とかにする。 | ||
- https://github.com/Exzrgs/LeetCode/pull/26/files#r1622122224 | ||
|
||
|
||
- `str()`は文字列表現を返す。 | ||
- `str(['a', 'e', 't'])`は`['a', 'e', 't']`という文字列を返す。 | ||
- 今回の辞書のキーとして一意なものがたまたまできたが、意図とは違った。 | ||
- https://docs.python.org/3/library/stdtypes.html#str | ||
- やりたいのは`"".join(['a', 'e', 't'])` | ||
- https://docs.python.org/3/library/stdtypes.html#str.join | ||
|
||
```python | ||
class Solution: | ||
def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
sorted_str_to_original = defaultdict(list) | ||
for s in strs: | ||
sorted_string = "".join(sorted(s)) | ||
sorted_str_to_strs[sorted_string].append(s) | ||
return list(sorted_str_to_strs.values()) | ||
``` | ||
|
||
# Step 3 | ||
|
||
- 入力文字列の個数をn、最大の文字列長をmとして | ||
- 時間計算量: O(n * mlogm)。 | ||
- 空間計算量: O(n) | ||
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. O(n * m)だと思います。 |
||
|
||
```python | ||
class Solution: | ||
def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
sorted_str_to_original = defaultdict(list) | ||
for s in strs: | ||
sorted_string = "".join(sorted(s)) | ||
sorted_str_to_original[sorted_string].append(s) | ||
return list(sorted_str_to_original.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.
Tuple を使うほうが気分がいい時もあるとは思います。表せている集合の範囲が素直かということかなと思います。
こういうところの速度は Python はそもそも C++ の50倍前後遅いのであまり気にしないかなと思います。
下は、"*" を区切り字としている例です。
https://discord.com/channels/1084280443945353267/1303605021597761649/1306562757315002389