Skip to content

387. First Unique Character In A String#17

Open
xbam326 wants to merge 3 commits into
mainfrom
387
Open

387. First Unique Character In A String#17
xbam326 wants to merge 3 commits into
mainfrom
387

Conversation

@xbam326
Copy link
Copy Markdown
Owner

@xbam326 xbam326 commented Jan 16, 2026

- https://docs.python.org/ja/3/library/functions.html#next
- https://docs.python.org/ja/3/library/functions.html#iter
- こういうのを使っていると上級者なんだろうなと思う
- 自分から書く選択肢に全くないのでなんとかしたい気持ち
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

私も知らなくて、調べて見つけた記憶があります。
dict.values() の一番目だけ欲しいけど、dict[1] とは書けないし、リストにしたら全要素コピーされて無駄だぞ…。何か方法があるんじゃないかな、と思って調べて突き当たりました。
私の経験からですが「何か方法があるんじゃないかな」は大抵の場合、方法があります。

# @lc code=start
class Solution:
def firstUniqChar(self, s: str) -> int:
charactor_to_index = dict()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

タイポしてますね。

Suggested change
charactor_to_index = dict()
character_to_index = dict()

Comment on lines +15 to +17
if charactor in charactor_to_index:
del charactor_to_index[charactor]
continue
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

頭から読んだとき、

  • なぜ辞書でヒットしたら、そこからインデックスを消しちゃうんだ?seen_characters に含まれているかの確認は先にしなくていいのか?
    • 正解は、ユニークな文字だけ を辞書に保存したいから
    • seen_characters には add しなくていいのか?
      • 実は、辞書に追加されているなら seen_characters にも入っている(これは後ろを読んで分かる)

のように、実際の動きを頭の中で頑張ってシミュレートする必要があり、短い行数のわりに大変に感じました。

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.

2 participants