[LeetCode][Python3] 387. First Unique Character in a String
2018. 11. 5. 23:46 |
프로그래밍/LeetCode
Problem :
https://leetcode.com/problems/first-unique-character-in-a-string/
My Solution :
class Solution:
def firstUniqChar(self, s):
"""
:type s: str
:rtype: int
"""
counter = [0]*26
for c in s:
counter[ord(c)-97] += 1
for i, c in enumerate(s):
if counter[ord(c)-97] == 1:
return i
return -1
Comment :
알파벳 소문자라는 제약이 있기 때문에 이렇게 풀었다. Dictionary를 활용한 버전은 살짝 바꾼 아래와 같다.
My Solution2 :
class Solution:
def firstUniqChar(self, s):
"""
:type s: str
:rtype: int
"""
counter = dict()
for c in s:
counter[c] = counter.get(c, 0) + 1
for i, c in enumerate(s):
if counter.get(c, 0) == 1:
return i
return -1
'프로그래밍 > LeetCode' 카테고리의 다른 글
[LeetCode][Python3] 19. Remove Nth Node From End of List (0) | 2018.11.09 |
---|---|
[LeetCode][Python3] 17. Letter Combinations of a Phone Number (0) | 2018.11.09 |
[LeetCode][Python3] 11. Container With Most Water (0) | 2018.11.07 |
[LeetCode][Python3] 108. Convert Sorted Array to Binary Search Tree (0) | 2018.11.06 |
[LeetCode][Python3] 350. Intersection of Two Arrays II (0) | 2018.11.05 |
[LeetCode][Python3] 344. Reverse String (0) | 2018.11.05 |
[LeetCode][Python3] 268. Missing Number (0) | 2018.11.04 |
[LeetCode][Python3] 242. Valid Anagram (0) | 2018.11.04 |
최근에 달린 댓글 최근에 달린 댓글