-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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]: | ||
return i | ||
return -1 | ||
|
||
``` | ||
for文で虱潰しに調べた.もっと良いアルゴリズムが有りそう. | ||
|
||
# Step2 | ||
他の人の解答を参照 | ||
https://github.com/rinost081/LeetCode/pull/14 | ||
https://github.com/Satorien/LeetCode/pull/15/ | ||
|
||
- Counterメソッドを使うと文字の出現回数を辞書のように保持できて便利 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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): | ||
aiueoriku marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if char_to_count[char]==1: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. 仰るとおりですね. |
||
return i | ||
return -1 | ||
``` | ||
Counterを使うほうが簡潔だし,処理速度も早い. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. そうですね。一応、この問題は LRU を連想してもいいかなと思います。 このあたりをなんとなく見ておいてもらえると嬉しいです。 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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回で済むように改変した. |
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.
スライスはコピーされるので、他の解き方よりは計算量が多くなっているはずです。
ryosuketc/leetcode_arai60#11 (comment)
char は s の文字列を左から見ているので、if 文でも s[:i] が左側にある方が個人的には自然な気がしました。趣味の範囲と思います。
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.
スライスのコピーの認識はありませんでした。ありがとうございます。sの見方も左からの方が自然ですね。
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.
str.find のところを読んだことがありますか?
https://docs.python.org/3/library/stdtypes.html#str.find
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.
読んだことありませんでした.確認致しました.