Skip to content

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
79 changes: 79 additions & 0 deletions 49. Group Anagrams.md
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
Copy link

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


```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)

Choose a reason for hiding this comment

The 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())
```