Skip to content

49. Group Anagrams#12

Open
tarinaihitori wants to merge 1 commit into
mainfrom
49-group-anagrams
Open

49. Group Anagrams#12
tarinaihitori wants to merge 1 commit into
mainfrom
49-group-anagrams

Conversation

@tarinaihitori
Copy link
Copy Markdown
Owner

Copy link
Copy Markdown

@seal-azarashi seal-azarashi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

各解法やそれぞれに至る過程での調査等、全体通してとてもよく出来ていると思いました。

強いて付け加えるなら、Leetcode では constraints として明記されているので問題ではないのですが、実際のプロダクトに対してこれを実装することを想定すると、引数 strs の要素には lowercase English letters しかないことが明記されていたり、そういった仕様が docstring にしっかり書かれているとより適切な実装になるのかなと思います。

Comment thread 49. Group Anagrams.md
https://github.com/rihib/leetcode/pull/8#:~:text=Python%E3%81%AB%E3%81%8A%E3%81%91%E3%82%8B%7B%7D,leetcode%2313%20(comment)

https://pylint.readthedocs.io/en/latest/user_guide/messages/refactor/use-list-literal.html
時間計算量:O(n ^ mlogm) strs の長さを n, string の長さを m とする
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

O(n * mlogm) ではないでしょうか?

def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
    anagrams = defaultdict(list)
    for string in strs:                             # n回のループ
        sorted_string_list = sorted(string)         # O(mlogm)
        sorted_string = "".join(sorted_string_list) # O(m)
        anagrams[sorted_string].append(string)      # O(1)
    return list(anagrams.values())                  # O(n)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

確かに累乗ではなく、O(n * mlogm) が正しいですね。
ご指摘ありがとうございます。

@Yoshiki-Iwasa
Copy link
Copy Markdown

他にも入力が英小文字のみで構成されるという前提の元、charのヒストグラムを作りそれをkeyとしたhashmapのvalueに文字列を格納していくという方法もあります

@tarinaihitori
Copy link
Copy Markdown
Owner Author

@Yoshiki-Iwasa

他にも入力が英小文字のみで構成されるという前提の元、charのヒストグラムを作りそれをkeyとしたhashmapのvalueに文字列を格納していくという方法もあります

これは思いつかなかったので、こちらも書いてみます。

@liquo-rice
Copy link
Copy Markdown

@Yoshiki-Iwasa

他にも入力が英小文字のみで構成されるという前提の元、charのヒストグラムを作りそれをkeyとしたhashmapのvalueに文字列を格納していくという方法もあります

これは思いつかなかったので、こちらも書いてみます。

バケットソートの要領ですね。

Copy link
Copy Markdown

@hayashi-ay hayashi-ay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

良いと思います

Comment thread 49. Group Anagrams.md
ちなみに dict の.values()はリストではなく、view_object を返す
https://docs.python.org/3/library/stdtypes.html#dict-views

普通の dict を使うと、キーの存在チェックと初期化が必要になるため、defaultdict を使用している。
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defaultdict 使わないならばどう書くかは一回考えておきましょう。

Copy link
Copy Markdown

@colorbox colorbox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

良さそうに思いました

@hroc135
Copy link
Copy Markdown

hroc135 commented Nov 16, 2024

拝見しました。

ついでに気になったので Python のハッシュ可能なものも軽く調べた

こういう心構えを自分も大事にしたいと思いました

Comment thread 49. Group Anagrams.md
```python
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
anagram_pairs = defaultdict(list)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pairだと1対の2つの要素という印象を受けてしまうので、groupの方が良いのではないかと思いました

Comment thread 49. Group Anagrams.md
Comment on lines +41 to +42
どちらも計算量的には O(n)だが、.join()はメモリの再割り当てが 1 回で済むように最適化されている?ので、
.join()のほうが定数倍早いのではないかと思う。(CPython の実装を見た)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants