Skip to content

Create 387. First Unique Character in a String.md#32

Merged
Mike0121 merged 1 commit intomainfrom
387.-First-Unique-Character-in-a-String
Nov 1, 2025
Merged

Create 387. First Unique Character in a String.md#32
Mike0121 merged 1 commit intomainfrom
387.-First-Unique-Character-in-a-String

Conversation

@Mike0121
Copy link
Copy Markdown
Owner

Copy link
Copy Markdown
Owner Author

@Mike0121 Mike0121 Jun 22, 2024

Choose a reason for hiding this comment

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

こちら下記でOdaさんが既に指摘済みでした。
TORUS0818/leetcode#17

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

index は start の引数が使えるので、頭から尻尾まで見ない手もありますね。
s.index(c, i + 1) など。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Odaさん、ありがとうございます。
enumerateにして、s.index(c, i + 1) == -1にすれば、少し効率的にできますね。
また、rindexを使わなくて良くなるので、indexさえ理解してれば伝わるのも良いと思いました。
頭に入れておきます。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@Mike0121
仮に英小文字以外の文字が入っていたらどうなるんでしょうか?

言語にもよりますが、リストへのindexアクセスは容易にクラッシュを引き起こすので気になりました。(確かPythonは負のindexを使えるんでしたっけ?)

もし考慮済みだったら一言コメントがあるといいかもしれません

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

char_histogram は英小文字の差しか見れないと思うので、alphabet_histogram とかでもいいかもしれません。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

@Yoshiki-Iwasa さん、 @goto-untrapped さん レビューコメントありがとうございます。
問題文の制約で、lower case English lettersのためこの書き方にしていますが、それ以外の入力があった場合はエラー処理を書くか、コメントがあったほうが親切ですね。また、list範囲外のindexは、pythonではIndexError: list index out of rangeが発生します。負の値もindexに使えますが、末尾から値を取る形になります。(ちょっと公式ドキュメントが見当たりませんでした。https://qiita.com/mo256man/items/ed8ea21bd0ae94e5ed48)

また、@goto-untrappedさんからコメントいただいた通り、変数名が良くない(広い)ので、alphabet_histogramの方が良いですね。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

oda さんか誰かが、英語圏の人が同僚になる想定で面接を受けましょうと言っていた気がします。英語圏の人にとってアルファベットが26文字なのは自明のはずなので、単に26を渡して配列を宣言してもいいのかなと思いました。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

すいません、投稿後すぐ考え直したのですが、翻って自分がひらがなの数をすぐ答えられるかと問われたら全然自信が無いなと思ったので、やはりこれで良いかも知れません...🫠
とはいえ、むしろアルファベットが26文字というのが自明でないのなら ord('z') - ord('a') + 1 の結果が何になるのかもすぐには分からないはずなので、例えば ALPHABET_NUMBER のような定数を宣言し、26 代入して使ってあげるとより親切になるかな、とも思いました。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

コメント有難うございます。そもそもちょっと練習のためにこの書き方を採用したというのが一番大きな理由になってしまうんですが、ALPHABET_NUMBER = 26が一番自分もわかりやすいですし、メンテナンスしやすいですね。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

あと min() が O(n) の操作をしているのも気になりますね...

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.

5 participants