Home LeetCode - 387. First Unique Character in a String
Post
Cancel

LeetCode - 387. First Unique Character in a String

387. First Unique Character in a String - easy

문제

Given a string, find the first non-repeating character in it and return it’s index.

If it doesn’t exist, return -1.

제한사항

입출력 예

1
2
3
4
5
6
7
Example :

s = "leetcode"
return 0.

s = "loveleetcode",
return 2.

풀이

  • Hash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
func firstUniqChar(s string) int {
    m := make(map[rune]int)
    
    for _, v := range(s) {
        m[v]++
    }
    
    for i, v := range(s) {
        if(m[v] < 2) {
            return i
        }
    }
    
    return -1
}
This post is licensed under CC BY 4.0 by the author.