Skip to content

Add 49. Group Anagrams.md #12

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 2 commits into
base: main
Choose a base branch
from
Open

Add 49. Group Anagrams.md #12

wants to merge 2 commits into from

Conversation

t0hsumi
Copy link
Owner

@t0hsumi t0hsumi commented Dec 16, 2024

class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
def count_char_frequency(string: str) -> Tuple[int, ...]:
char_frequency = [0] * 26
Copy link

Choose a reason for hiding this comment

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

alphabet_count = 26
char_frequency = [0] * alphabet_count

としてマジックナンバーを避けると良いと思いました

frequency = Counter(s)
lowercase_frequency = tuple(
frequency[lowercase] for lowercase in string.ascii_lowercase
)
Copy link

Choose a reason for hiding this comment

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

感想: こういう書き方もあるんですね

Copy link

Choose a reason for hiding this comment

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

リストの内包表記の部品ですよ。

Copy link

Choose a reason for hiding this comment

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

あー、上は誤りですね。ジェネレータ式は文法上内包表記とは別物でした。
https://docs.python.org/3/reference/expressions.html#generator-expressions
https://peps.python.org/pep-0289/

def count_char_frequency(string: str) -> Tuple[int, ...]:
char_frequency = [0] * 26
for char in string:
index = ord(char) - ord('a')
Copy link

Choose a reason for hiding this comment

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

https://docs.python.org/3/library/functions.html#ord
文字コードを一回確認しておいてください。
たとえば、C++ だと、ASCII とは限らないのでアルファベットが連続しているとは限りません。
https://discord.com/channels/1084280443945353267/1301587996298182696/1309561565187538964
(コードの正誤ではなく)確認したい気持ちがあるかどうかを大事にしてください。

Copy link
Owner Author

Choose a reason for hiding this comment

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

asciiでa-zが連続していることは知っていましたが、連続していない文字コードがあることは一切想定していなかったです。

Copy link

Choose a reason for hiding this comment

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

Python ord は Unicode のコードポイントが返ってくるので連続しているとしていいんです。つまり、結果的には同じコードになるんですが、しかし「Python ord は Unicode のコードポイントだからアルファベット部分は ASCII と同じ、だから連続しているとしてよい」と思って書いたほうがいいでしょう。
C / C++ だと、この保証はないのですが、しかし、そういう文字コードは稀なので「意図的にサポートを切り捨てた」と思っていればいいです。


- https://github.com/katataku/leetcode/pull/11/files
- `list(dict.values())`としていた
- `sorted_string = ''.join(sorted(string))`としていた。これくらいなら、`sorted(string)`を別変数でおかなくても十分読めるなと感じた
Copy link

Choose a reason for hiding this comment

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

一応、str(sorted(string)) が ''.join(sorted(string)) とは異なるものであるという認識があるかの確認はしておきます。

Copy link
Owner Author

Choose a reason for hiding this comment

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

違いについて説明せよと言われてもできなそうなので、調べました。

文字列をsortしたリストを引数にした場合、どちらも返す値は変わらないので今回はどっちでもいいのかなぁくらいの感想でした。

sorted(string)

str(sorted(string))

''.join(sorted(string))

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.

良さそうと思いました

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.

4 participants