Post

Leetcode - 387. First Unique Character in a String

Leetcode - 387. First Unique Character in a String

Hits

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

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
func firstUniqChar(s string) int {
    freqMap := make(map[rune]int)

    for _, char := range s{
        freqMap[char]++
    }

    for ind, char := range s{
        if freqMap[char]==1{
            return ind
        }
    }
    return -1
}
This post is licensed under CC BY 4.0 by the author.