Open
Conversation
mamo3gr
reviewed
Feb 14, 2026
Comment on lines
+76
to
+83
| - leetcode の解答 | ||
| - シンプルに `s` の文字を数えて map に保存する | ||
| - rune でも対応可能なので、汎用性が高い | ||
| - もう一度 `s` を走査する | ||
| - そのときに回数が1であれば、そのときのインデックスが答えとなる | ||
| - 走査が終わったら1回しか登場しない文字はなかったということなので -1 を返す | ||
| - 時間計算量: O(n) | ||
| - 空間計算量: O(1) |
There was a problem hiding this comment.
two-passになりますが、コードはかなりシンプルになるので、これはこれでアリだなあと思いました。
Owner
Author
There was a problem hiding this comment.
ありがとうございます。
そうですね、まず最初に思いつくアルゴリズムとしても自然だと思いました。
(私は思いつかなかったのですが)
複雑なことをまず試しがちなので、「手作業でやってみる」ことから始めるよう練習します。
TakayaShirai
left a comment
There was a problem hiding this comment.
全体的に読みやすかったです。時間計算量を求めるだけでなく、具体的な計算時間まで概算されるとさらに良いと思います!
| - そのときに回数が1であれば、そのときのインデックスが答えとなる | ||
| - 走査が終わったら1回しか登場しない文字はなかったということなので -1 を返す | ||
| - 時間計算量: O(n) | ||
| - 空間計算量: O(1) |
There was a problem hiding this comment.
ここの空間計算量が O(1) で想定されているのは、アルファベットのみしか来ないという条件だからでしょうか。
Owner
Author
There was a problem hiding this comment.
ありがとうございます。
記載いただいている通りで、問題設定で文字種別が固定されているため O(1) としました。
一般化すると 0(n) になると思います。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
今回の問題
First Unique Character in a String - LeetCode
使用言語
Go
次に解く問題
Subarray Sum Equals K - LeetCode