본문 바로가기
LeetCode

387. First Unique Character in a String 문제풀이

by O_x 2022. 7. 19.
387. First Unique Character in a String
Easy
5340207Add to ListShare

Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.

단 한번만 쓰여진 문자형을 찾아 그 index를 retrun 하면 된다. 

 

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.

 

class Solution {
public:
    int firstUniqChar(string s) {
        unordered_map <char,int> mp;
        for(int i=0; i<s.size(); i++) {
            mp[s[i]]++; // ma[s[i]]의 char인 key값 에 접근하여 value값을 ++해준다는 의미이다.
        }
        for(int i=0; i<s.size(); i++) {
            if(mp[s[i]] == 1) { //중복수가 있는 char들은 2이상
                return i;
            }
        }
        return -1; //아닌경우 -1
    }
};
 

https://open.kakao.com/o/gsMhUFie

 

C/C++/C# 언리얼/유니티 /질문

#C++#C#언리얼#게임개발#질문#개발#자료구조#백준#프로그래머스#c#유니티#unity#enreal

open.kakao.com

운영중인 카톡방입니다.

'LeetCode' 카테고리의 다른 글

242. Valid Anagram C++문제 풀이  (0) 2022.07.20
20. Valid Parentheses 문제풀이  (0) 2022.07.13
415. Add Strings 문제 c++  (0) 2022.07.10
125. Valid Palindrome 문제  (0) 2022.07.10
796. Rotate String 문제  (0) 2022.07.07

댓글