387. First Unique Character in a String#16
Open
X-XsleepZzz wants to merge 2 commits into
Open
Conversation
Added problem description and examples for the First Unique Character in a String.
Added Python implementation for finding the first unique character in a string, including explanations and complexity analysis.
nodchip
reviewed
Mar 2, 2026
| def firstUniqChar(self, s: str) -> int: | ||
| char_to_index = {} | ||
|
|
||
| for index, char in enumerate(s): |
There was a problem hiding this comment.
char は他の言語で予約語となっている場合がありますので、避けたほうが無難だと思います。
| continue | ||
| char_to_index[char] = index | ||
|
|
||
| valid_char_list = [] |
There was a problem hiding this comment.
ここでもう一度
for index, char in enumerate(s):で回して、 char_to_index に int の値が含まれているものを返すという書き方もできそうです。ただ、そうなると、そもそも文字からインデックスへのマッピングを保持するより、それぞれの文字が南海出現したかをカウントして、 1 回のみのものを返す、としたほうがシンプルだとも思います。
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.
Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.
Example 1:
Input: s = "leetcode"
Output: 0
Explanation:
The character 'l' at index 0 is the first character that does not occur at any other index.
Example 2:
Input: s = "loveleetcode"
Output: 2
Example 3:
Input: s = "aabb"
Output: -1
Constraints:
1 <= s.length <= 105
s consists of only lowercase English letters.
url: https://leetcode.com/problems/first-unique-character-in-a-string/description/