Skip to content

Conversation

huyfififi
Copy link
Owner

@huyfififi huyfififi self-assigned this Apr 14, 2025
@huyfififi huyfififi force-pushed the leetcode-242-valid-anagram branch from c3e3c10 to 7227940 Compare April 14, 2025 03:04
@huyfififi huyfififi marked this pull request as ready for review April 14, 2025 03:08
for c in t:
t_counter[ord(c) - ord("a")] += 1

for i in range(26):
Copy link
Owner Author

Choose a reason for hiding this comment

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

よく考えたらここは

return s_counter == t_counter

でよかったですね

- 解法3. Hash Tableで各文字の個数をカウントして比較
- 解法2では入力にlowercaseのalphabetを想定しているが、文字一般をサポートすることができない。
- Time Complexity: O(n + m) (1回ずつ文字列を走査するため)
- Space Complexity: O(n + m) (ユニークな文字が最大でn + m種類ある場合)

Choose a reason for hiding this comment

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

lengthが同じ場合はO(n)と書けますが、それはさておき、kzhra-sanなどのように引き算で解くとSpaceはnまたはmのどちらかだけで済みますね。
このとき、片方で作った辞書or配列から減算していって、途中で負になったらその時点でFalse、最後まで負になることがなければTrueという最後までやらない方法もあります。
kzhra-sanなどの方法の亜種で、Time Complexityのオーダーは変わらないですが、引数によっては最後までやらない分だけ早くなるパターンもありそうです。この問題だと読みにくくなりますが、他の問題でこの考え方を使える場合があると思いました。

Copy link
Owner Author

Choose a reason for hiding this comment

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

コメントありがとうございます!早めに処理が抜けられるパターンに気を配るようにしますね

for c in t:
t_char_count[c] += 1

return s_char_count == t_char_count
Copy link

@sasanquaneuf sasanquaneuf Apr 14, 2025

Choose a reason for hiding this comment

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

ちなみに環境や条件によっては、defaultdictの方法とlistの方法でほぼ差が出なかったり、defaultdictのほうが早いケースもあったりするようです。(メカニズムをちゃんと把握はしていません)

Copy link

Choose a reason for hiding this comment

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

分からないですが、Python は変数からオブジェクトを取り出すのも hash map なので、dict を引くコストが相対的に小さい感じはしますね。

- [unicode.normalize](https://docs.python.org/3/library/unicodedata.html#unicodedata.normalize)
- [azriel1rf-san](https://github.com/azriel1rf/leetcode-prep/pull/2)
- `collections.Counter`を使った手法
- [GoogleのStyle Guide](https://google.github.io/styleguide/pyguide.html#316-naming)で"Avoid abbreviation."とされているのは知らなかった。`count`を`cnt`としているコードをよく見かけるが、省ける文字数もそこまで大きくなく、普通に省略せず書いた方がわかりやすい、というのは私も同意見である。
Copy link

Choose a reason for hiding this comment

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

いや、まあ、昔、といっても90年代くらいでも、長さの制限があって30-40文字以降は区別しないなどがありましたね。DOS はファイル名の制限が8文字+拡張子3文字だったんですよ。そういうリソースの制約があった時代の名残で、あんまり長いものは好まれなかったんですかね。

Copy link
Owner Author

Choose a reason for hiding this comment

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

そうなんですね、そういった歴史的な経緯を見ると確かに、できるだけ名前の長さを削ることも一理ありますね。

Comment on lines +9 to +10
for c in t:
t_counter[ord(c) - ord("a")] += 1
Copy link

Choose a reason for hiding this comment

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

同じことを2度したら関数にすることを考えてもいいでしょう。


# Step 3

`step2_collections_counter.py`と変わらなかったのでコードは省略。
Copy link

Choose a reason for hiding this comment

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

同じものでも貼っておいたらいいと思いますよ。

Copy link
Owner Author

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
Owner Author

Choose a reason for hiding this comment

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

了解です。では次から含めるようにしますね。

for c in t:
t_char_count[c] += 1

return s_char_count == t_char_count
Copy link

Choose a reason for hiding this comment

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

分からないですが、Python は変数からオブジェクトを取り出すのも hash map なので、dict を引くコストが相対的に小さい感じはしますね。

@huyfififi huyfififi merged commit 377f716 into main Apr 15, 2025
@huyfififi huyfififi deleted the leetcode-242-valid-anagram branch April 15, 2025 05:45

# Step 2

- [colorbox-san](https://github.com/colorbox/leetcode/pull/9)

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