반응형
Notice
Recent Posts
Recent Comments
- Today
- Total
작심삼일
[LeetCode] 387 | First Unique Character in a String | Python 본문
728x90
반응형
문제 링크: https://leetcode.com/problems/first-unique-character-in-a-string/
문제
Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.
조건
- 1 <= s.length <= $10^5$
- s consists of only lowercase English letters.
내 풀이
코드
class Solution:
def firstUniqChar(self, s: str) -> int:
unique, common = {}, []
for idx, char in enumerate(s):
if char not in unique and char not in common:
unique[char] = idx
elif char in unique:
common.append(char)
unique.pop(char)
return unique[list(unique.keys())[0]] if unique.keys() else -1
728x90
반응형
'스터디 > 코테' 카테고리의 다른 글
[LeetCode] 1338 | Reduce Array size to The Half | Python (0) | 2022.08.18 |
---|---|
[LeetCode] 804 | Unique Morse Code Words | Python (0) | 2022.08.17 |
[LeetCode] 98 | Validate Binary Search Tree | Python (0) | 2022.08.11 |
[LeetCode] 108 | Convert Sorted Array to Binary Search Tree | Python (0) | 2022.08.10 |
[LeetCode] 823 | Binary Trees With Factors | Python (0) | 2022.08.09 |
Comments