Skip to content

First Unique Character in a String.md #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 1 commit 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
100 changes: 100 additions & 0 deletions 387. First Unique Character in a String.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
URL: https://leetcode.com/problems/kth-largest-element-in-a-stream/description/

# Step 1

- 実装時間: 10分
- len(s)をnとして
- 時間計算量: O(n)
- 空間計算量: O(n)

```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
```

- characterというより、C言語のchar型のイメージであえて英単語の省略形を選んだ

# Step 2

- 参考にしたURL
- https://github.com/Exzrgs/LeetCode/pull/9
- https://github.com/SuperHotDogCat/coding-interview/pull/22
- https://github.com/hayashi-ay/leetcode/pull/28/files
- https://github.com/su33331/practice/pull/2
- https://github.com/fhiyo/leetcode/pull/18
- https://github.com/TORUS0818/leetcode/pull/17
- https://github.com/nittoco/leetcode/pull/20
- https://github.com/kazukiii/leetcode/pull/16
- https://github.com/Mike0121/LeetCode/pull/32
- https://github.com/Yoshiki-Iwasa/Arai60/pull/14
- https://github.com/seal-azarashi/leetcode/pull/15
- https://github.com/hroc135/leetcode/pull/15
- https://github.com/ryoooooory/LeetCode/pull/20
- https://github.com/tarinaihitori/leetcode/pull/15
- https://github.com/philip82148/leetcode-arai60/pull/4
- https://github.com/colorbox/leetcode/pull/29
- https://github.com/Hurukawa2121/leetcode/pull/15

- 追加質問でありそうだなと思ったやつ
- Counterを使わずに書くと?
Copy link

Choose a reason for hiding this comment

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

余力があれば Counter の内部実装を読むといいと思うのと、LinkedHashMap のようなものを使う実装は見ておくといいでしょう。

- 自分で辞書を作る
- その場合、defaultdictを使わずに書くと?
- 26文字char配列を使う方法
- 辞書を作らないのであれば、左右から検索して最初に見つかった位置が一致していればunique, という探し方もできる。

```python
class Solution:
def firstUniqChar(self, s: str) -> int:
for c in s:
if s.rindex(c) == s.index(c):
return s.index(c)
return -1
```

- > `return next(iter(char_to_first_appear_index.values()))`
- 一瞬、「リストにして`[0]`でいいじゃん?」と思ったけど、リストを作らずにiteratorのまま次の要素だけ持ってきているので効率的。
- 見たことない書き方だったけど、効率的な書き方だと思って感動。
- https://github.com/hayashi-ay/leetcode/pull/28/files

- > c は、私は許容です。char は、C/C++/Java などで予約語なので、私は避けます。
- 僕がstep1を解いてる時も、「文字コードから文字に変換するbuilt-inの名前はchrだっけcharだっけ。それと被らないようにしないと。」と、一度考えてた。これは脳のリソースを使ってるということ。
- https://github.com/su33331/practice/pull/2/files#r1615489250

- > -1 がどのような意味を持つのかコードを見るだけだと分からないので、定数にして役割に準じた名前をつけたり、コードコメントを残すといったことをした方がいいように思いました。
- https://github.com/Exzrgs/LeetCode/pull/9/files#r1703858740
- > `return -1; // Not Found`
- https://github.com/colorbox/leetcode/pull/29/files#r1861038507

- grueやbleenのはなし
- https://github.com/fhiyo/leetcode/pull/18/files#r1629654317

```python
class Solution:
def firstUniqChar(self, s: str) -> int:
char_to_frequency = Counter(s)
for i, c in enumerate(s):
if char_to_frequency[c] == 1:
return i
return -1 # Not found
```

# Step 3

- len(s)をnとして
- 時間計算量: O(n)
- 空間計算量: O(n)

```python
class Solution:
def firstUniqChar(self, s: str) -> int:
char_to_frequency = Counter(s)
for i, c in enumerate(s):
if char_to_frequency[c] == 1:
return i
return -1 # Not found
```