Skip to content

387. First Unique Character in a String #14

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
Show file tree
Hide file tree
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
Binary file modified 60 LeetCode problems to solve for coding interview.xlsx
Binary file not shown.
69 changes: 69 additions & 0 deletions lc387.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# step1
思考ログ
- 2つのpointerを使って走査すれば最悪計算時間はO(n^2)かかるが単純だと思った.

```python
class Solution:
def firstUniqChar(self, s: str) -> int:
seen = set()
for i in range(len(s)):
if s[i] in seen:
continue
j = i + 1
is_repeat = False
while(j < len(s)):
Copy link

Choose a reason for hiding this comment

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

このwhileはfor j in range(i + 1, len(s))にしたいと思いました

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文とwhile文が混ざっていますね. 統一するように意識したいと思います

Choose a reason for hiding this comment

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

一応、外側のかっこは不要ですね。

if s[j] == s[i]:
is_repeat = True
j += 1

if is_repeat is False:

Choose a reason for hiding this comment

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

while-else や for-else もつかえるでしょうか。

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
Comment on lines +12 to +20
Copy link

Choose a reason for hiding this comment

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

ここの部分、find に start をつけたものと同じではないでしょうか。
https://docs.python.org/3/library/stdtypes.html#str.find
(時々ライブラリーを確認してみましょう。)

seen.add(s[i])

return -1
```

# step2
- step1をchatgptに投げた結果, if s[j] == s[i]:のあとにbreakを入れるべきという指摘を受けた. その通りだと思った.
参考にした他の方のPR
- https://github.com/potrue/leetcode/pull/15/files?short_path=5e4287a#diff-5e4287a02e3f11dbbbe1217cb91b9370bcf7798cdde8ce6e7c1729817e53c0fc
- 前回同様,私のstep1での実装をライブラリ(Counterとfind)で簡潔に解いている実装だった.明らかにこちらの方が読みやすい.
```python
class Solution:
def firstUniqChar(self, s: str) -> int:
str_to_count = Counter(s)
for character in s:
if str_to_count[character] == 1:
return s.find(character)

return -1
```
- enumerateで場所を特定する方法
```python
class Solution:
def firstUniqChar(self, s: str) -> int:
str_to_count = Counter(s)
for i, character in enumerate(s):
if str_to_count[character] == 1:
return i

return -1
```
- https://github.com/nktr-cp/leetcode/pull/16/files?short_path=0c860cd#diff-0c860cd754249868513e4f9054206317fa33d0f548fc3896ac2b3e11822fd852
- c++での実装、ただ眺めていた

- https://github.com/mura0086/arai60/pull/19/files#diff-5ec7c3c87171edf4d61e9eb79fd926cafa27caf068da7474222897c8e9e7ab96
- [lru_cache](https://docs.python.org/ja/3.13/library/functools.html#functools.lru_cache)(Least Recently Used cache)という概念を初めて知った.
Copy link

@nktr-cp nktr-cp May 21, 2025

Choose a reason for hiding this comment

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

LRUはOSのページングシステム,CPUやディスクのキャッシュの勉強をしていてもみかけるアルゴリズムだと思うので,覚えておいてもいいかもしれないです


# step3
```
class Solution:
def firstUniqChar(self, s: str) -> int:
char_to_count = Counter(s)

for i, character in enumerate(s):
if char_to_count[character] == 1:
return i

return -1
```