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

Group Anagrams.md #11

wants to merge 1 commit into from

Conversation

katataku
Copy link
Owner

@katataku katataku commented Dec 9, 2024

Copy link

@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.

良いと思います。dictのkeyはtupleでも良いかもしれないですね。

@katataku
Copy link
Owner Author

katataku commented Dec 9, 2024

dictのkeyはtupleでも良いかもしれないですね。

ありがとうございます!ちなみに使い分けのtipsとかあったりしますか?
なんとなく、「今回の使い方だとhashableだったらいいから、どっちでもいいかなー」ぐらいに思ったりするんですが、他に考えるポイントとかあれば伺いたいです!

Copy link

@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.

良さそうに見えました

@hayashi-ay
Copy link

なんとなく、「今回の使い方だとhashableだったらいいから、どっちでもいいかなー」ぐらいに思ったりするんですが、他に考えるポイントとかあれば伺いたいです!

うーん、今回なら自分もどっちでも良いと思います。一応、観点としては以下ですかね

  • ソースコードとしての意図の伝わりやすさ
     - 今回は好みの範囲かもしれないですね
  • パフォーマンス観点
    • 文字列のjoin vs tupleの処理
    • これも今回はそんなに変わらないと思います。
  • メモリ使用量

- 今回の辞書のキーとして一意なものがたまたまできたが、意図とは違った。
- 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

@katataku
Copy link
Owner Author

ありがとうござます。

Tuples are also used for cases where an immutable sequence of homogeneous data is needed (such as allowing storage in a set or dict instance).

改めてdocsを見てみると、このようにあったので、tupleの方が想定ユースケース的な使い方であり"素直に表現する"という意図に沿ってるのかなと理解しました

https://docs.python.org/3/library/stdtypes.html#tuples


- 入力文字列の個数を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)だと思います。

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.

5 participants