Skip to content

387. First Unique Character in a String#15

Open
TakayaShirai wants to merge 1 commit intomainfrom
387_first_unique_character_in_a_string
Open

387. First Unique Character in a String#15
TakayaShirai wants to merge 1 commit intomainfrom
387_first_unique_character_in_a_string

Conversation

@TakayaShirai
Copy link
Copy Markdown
Owner

@TakayaShirai TakayaShirai self-assigned this Jan 11, 2026
//
// https://discord.com/channels/1084280443945353267/1233603535862628432/1238208008182562927
// 不変条件を意識するのは大事。LinkedHashMap があれば、一つ気にしなければいけない要素を減らせる。
// Dart の Map はデフォルトで LinkedHashMap になっているらしい。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Java の LinkedHashMap は、要するに LinkedList + HashMap というデータ構造ですが、取り除くのは後でもいいので Queue と HashMap でもいけるという感じですね。

// 自分は Set を用いて重複を判定していたが、counter での重複の方がコードが簡潔に済んで良い。
//
// https://github.com/t0hsumi/leetcode/pull/15#discussion_r1930362913
// 重複がないことの証明として、「左から読んだときと右から読んだときで、添字が変わらない」ことを使っていた。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Python とかだと、文字を探すのはネイティブコードで走るので何度も走査していてもそこそこ速いと思います。

var counter = <String, int>{};

for (var i = 0; i < s.length; i++) {
counter[s[i]] = (counter[s[i]] ?? 0) + 1;
Copy link
Copy Markdown

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.

3 participants