Skip to content

Create First Unique Character in a String.md #5

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

Conversation

aiueoriku
Copy link
Owner

class Solution:
def firstUniqChar(self, s: str) -> int:
for i, char in enumerate(s):
if char not in s[i+1:] and char not in s[:i]:

Choose a reason for hiding this comment

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

スライスはコピーされるので、他の解き方よりは計算量が多くなっているはずです。
ryosuketc/leetcode_arai60#11 (comment)
char は s の文字列を左から見ているので、if 文でも s[:i] が左側にある方が個人的には自然な気がしました。趣味の範囲と思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

スライスのコピーの認識はありませんでした。ありがとうございます。sの見方も左からの方が自然ですね。

Copy link

Choose a reason for hiding this comment

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

str.find のところを読んだことがありますか?
https://docs.python.org/3/library/stdtypes.html#str.find

str.find(sub[, start[, end]])

Copy link
Owner Author

Choose a reason for hiding this comment

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

読んだことありませんでした.確認致しました.

https://github.com/rinost081/LeetCode/pull/14
https://github.com/Satorien/LeetCode/pull/15/

- Counterメソッドを使うと文字の出現回数を辞書のように保持できて便利

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/collections.html#collections.Counter

Copy link
Owner Author

Choose a reason for hiding this comment

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

勘違いしていました。ご指摘ありがとうございます。

class Solution:
def firstUniqChar(self, s: str) -> int:
for i, char in enumerate(s):
if char not in s[i+1:] and char not in s[:i]:
Copy link

Choose a reason for hiding this comment

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

str.find のところを読んだことがありますか?
https://docs.python.org/3/library/stdtypes.html#str.find

str.find(sub[, start[, end]])

return i
return -1
```
Counterを使うほうが簡潔だし,処理速度も早い.
Copy link

Choose a reason for hiding this comment

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

そうですね。一応、この問題は LRU を連想してもいいかなと思います。

このあたりをなんとなく見ておいてもらえると嬉しいです。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.hztxgiufh8yd

Copy link
Owner Author

Choose a reason for hiding this comment

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

LRUというキャッシュ管理アルゴリズムなるものが存在するのですね.勉強になります.ありがとうございます.

for char in s:
char_to_count[char] += 1
for i, char in enumerate(s):
if char_to_count[char]==1:
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.

仰るとおりですね.

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