-
Notifications
You must be signed in to change notification settings - Fork 0
Leetcode 242. Valid Anagram #7
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
Conversation
c3e3c10
to
7227940
Compare
for c in t: | ||
t_counter[ord(c) - ord("a")] += 1 | ||
|
||
for i in range(26): |
There was a problem hiding this comment.
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種類ある場合) |
There was a problem hiding this comment.
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のオーダーは変わらないですが、引数によっては最後までやらない分だけ早くなるパターンもありそうです。この問題だと読みにくくなりますが、他の問題でこの考え方を使える場合があると思いました。
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ちなみに環境や条件によっては、defaultdictの方法とlistの方法でほぼ差が出なかったり、defaultdictのほうが早いケースもあったりするようです。(メカニズムをちゃんと把握はしていません)
There was a problem hiding this comment.
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`としているコードをよく見かけるが、省ける文字数もそこまで大きくなく、普通に省略せず書いた方がわかりやすい、というのは私も同意見である。 |
There was a problem hiding this comment.
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文字だったんですよ。そういうリソースの制約があった時代の名残で、あんまり長いものは好まれなかったんですかね。
There was a problem hiding this comment.
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_counter[ord(c) - ord("a")] += 1 |
There was a problem hiding this comment.
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`と変わらなかったのでコードは省略。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同じものでも貼っておいたらいいと思いますよ。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます!単純な興味なのですが、レビューする方から見て同じ情報が繰り返されるのは冗長で、差分の確認も少し負荷がかかったりしませんか?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
あー、読む速度は自然言語とそんなに変わらないんですよ。
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
分からないですが、Python は変数からオブジェクトを取り出すのも hash map なので、dict を引くコストが相対的に小さい感じはしますね。
|
||
# Step 2 | ||
|
||
- [colorbox-san](https://github.com/colorbox/leetcode/pull/9) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
目から鱗でした、、!
https://leetcode.com/problems/valid-anagram/