My solution with Go in 22ms


  • 0
    K

    Use a two-dimensional array to record the count of letter appearance and the index of letter

    func firstUniqChar(s string) int {
        if len(s) == 0 {
            return -1
        }
        arr := make([][2]int, 256)
        for index, val := range s {
            arr[val][0]++
            arr[val][1] = index
        }
        i := -1
        for _, val := range arr {
            if val[0] == 1{
              if i == -1 {
                i = val[1]
              } else {
                i = int(math.Min(float64(val[1]), float64(i)))
              }
            }
        }
        return i
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.