saurus2
Saurus2
saurus2
전체 방문자
오늘
어제
  • 분류 전체보기
    • 개발
      • AJAX
    • ML Ops
    • Profile
    • 음식점
    • 배낭여행
    • 컴퓨터공학
      • 알고리즘 공부
      • C++
      • Sever 스터디
      • Java spring
      • 알고리즘 _ 문제해결
      • 딥러닝
      • Java 정리
      • Python
      • LeetCode 1000
      • Machine Learning Study
      • Sign language Detection Pro..
      • LeetCode Solutions
    • 비콘
    • 데일리 리포트
    • 유학일기
      • 영어 공부
      • Daily
    • AI Master Degree
      • Data Mining
      • AI and Data engineering
      • Math Foundations for Decisi..
      • Natural Language Processing

블로그 메뉴

  • 홈
  • 태그
  • 미디어로그
  • 위치로그
  • 방명록

공지사항

인기 글

태그

  • 딕셔너리
  • 리트코드
  • DFS
  • Python
  • 취업준비
  • 알고리즘문제해결
  • LeetCode
  • 개발자
  • 백준
  • 파이썬
  • c++
  • 문제해결능력
  • 개발자 취업준비
  • 온라인저지
  • two pointer
  • 알고리즘
  • 릿코드
  • BFS
  • 딥러닝
  • 취준

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
saurus2

Saurus2

컴퓨터공학/LeetCode 1000

[LeetCode] 387. First Unique Character in a String

2022. 8. 16. 11:48

387. First Unique Character in a String

Easy

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

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.

문제 풀이

문제 접근

  1. 문장의 길이가 10^5 이기 때문에 Linear 시간 안에 풀어야한다. O(n) 

풀이

  1. 해쉬맵을 사용해서 글자가 몇개인지 모두 확인해서 답을 구하자.
  2. 어느 문자가 몇개있는지 알려면 모든 숫자를 세어봐야한다. 
  3. defaultdict(int) 로 세어도되고, Counter 를 사용해서 답을 구해도 된다. 

소스 코드

class Solution:
    def firstUniqChar(self, s: str) -> int:
        hashmap = Counter(s)
        for index, l in enumerate(s):
            if hashmap[l] == 1:
                return index
        return -1
저작자표시 (새창열림)

'컴퓨터공학 > LeetCode 1000' 카테고리의 다른 글

[LeetCode] 804. Unique Morse Code Words  (0) 2022.08.17
[LeetCode] 5. Longest Palindromic Substring  (0) 2022.08.16
[LeetCode] 13. Roman to Integer  (0) 2022.08.16
[LeetCode] 90. Subsets II  (0) 2022.08.14
[LeetCode] 191. Number of 1 Bits  (0) 2022.08.14
    '컴퓨터공학/LeetCode 1000' 카테고리의 다른 글
    • [LeetCode] 804. Unique Morse Code Words
    • [LeetCode] 5. Longest Palindromic Substring
    • [LeetCode] 13. Roman to Integer
    • [LeetCode] 90. Subsets II
    saurus2
    saurus2
    Simple is Best

    티스토리툴바