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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Step1
何も見ずに解く
```python
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.

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

return i
return -1

```
for文で虱潰しに調べた.もっと良いアルゴリズムが有りそう.

# Step2
他の人の解答を参照
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.

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

- Counterメソッドを持ちいらずに辞書を自分で定義して出現回数を保持するということも出来る

# Step3
他の人の解答を参考に解き直す
```python
class Solution:
def firstUniqChar(self, s: str) -> int:
char_to_count = Counter(s)
for i, char in enumerate(s):
if char_to_count[char] == 1:
return i
return -1
```
```python
class Solution:
def firstUniqChar(self, s: str) -> int:
char_to_count = defaultdict(int)
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.

仰るとおりですね.

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というキャッシュ管理アルゴリズムなるものが存在するのですね.勉強になります.ありがとうございます.


# Step4
コメントを受けて再度解き直す
```python
class Solution:
def firstUniqChar(self, s: str) -> int:
char_to_count = defaultdict(int)
char_to_index = {}
for index, char in enumerate(s):
char_to_count[char] += 1
if char not in char_to_index:
char_to_index[char] = index

min_index = len(s)
for char, count in char_to_count.items():
if count == 1:
if char_to_index[char] < min_index:
min_index = char_to_index[char]
return min_index if min_index != len(s) else -1
```
char_to_indexに各文字の最初のインデックスを保持して,文字列全体に対するfor文の回数を1回で済むように改変した.