Skip to content

Conversation

nittoco
Copy link
Owner

@nittoco nittoco commented Jun 16, 2024

https://leetcode.com/problems/first-unique-character-in-a-string/description/
Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.

Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.
- torusさんへのodaさんのコメントを見る限り、十分高速ではあるらしい
- [コード](https://github.com/python/cpython/blob/main/Python/bltinmodule.c#L1796C1-L1796C8) も流し読み やはり全要素をなめてる
- next iterを使えば2回ループは避けれる(無理矢理感)
- secoundは微妙な気がしてきた。seenとかduplicatedの方が良かったかも。
Copy link

Choose a reason for hiding this comment

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

ささいなことですが secound のところ、2nd の意味であれば second だと思います。
https://dictionary.cambridge.org/dictionary/english/second

また、duplicate には名詞の用法もあるようなので、変数名を duplicates と名詞形にするのもよさそうだと思いました。
https://dictionary.cambridge.org/dictionary/english/duplicate

Copy link
Owner Author

Choose a reason for hiding this comment

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

これ恥ずかしいです😇
確かに検索したら、duplicated_nameみたいに後に名詞が来る場合が多いですね。ありがとうございます
https://source.chromium.org/search?q=duplicated&ss=chromium%2Fchromium%2Fsrc&start=21

## Step2

- https://github.com/shining-ai/leetcode/pull/15
- LinkedHashMapが、多分Step1の最後で言ったようなことができるデータ構造。後で、LinkedHashMapの実装をしてみる
Copy link

Choose a reason for hiding this comment

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

本当は平衡木を使う方法もあるんですが、Python だと標準にはないようですね。
Ryotaro25/leetcode_first60#16 (comment)

@nittoco nittoco changed the title 387. First Unique Character in the String 387. First Unique Character in a String Jun 21, 2024
self.sentinel = LinkedListDictNode()
self.sentinel.prev = self.sentinel
self.sentinel.next = self.sentinel
self.last = self.sentinel
Copy link

Choose a reason for hiding this comment

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

これ、常に self.sentinel.prev であるという理解でいいですか? (条件分岐を減らせますか?)

Copy link
Owner Author

Choose a reason for hiding this comment

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

確かにその方が素直かつ分岐も減らせますね。ありがとうございます

Comment on lines +125 to +133
class Solution:
def firstUniqChar(self, s: str) -> int:
char_to_freq = defaultdict(int)
for c in s:
char_to_freq[c] += 1
for i, c in enumerate(s):
if char_to_freq[c] == 1:
return i
return -1

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.

4 participants