Skip to content

Instantly share code, notes, and snippets.

@luojiyin1987
Created August 16, 2017 14:32
Show Gist options
  • Save luojiyin1987/9066bb58388bb8336c31ea0f7a902c23 to your computer and use it in GitHub Desktop.
Save luojiyin1987/9066bb58388bb8336c31ea0f7a902c23 to your computer and use it in GitHub Desktop.
First Unique Character in a String
func firstUniqChar(s string) int { //it is fast
m := [26]int{}
for _, ch := range s {
m[int(ch-'a')]++
}
for k, ch := range s {
if m[int(ch-'a')] == 1 {
return k
}
}
return -1
}
//--------------------------------------
func firstUniqChar(s string) int { //it is slow
temp := make(map[int32]int)
for _, v := range s {
temp[v]++
}
for k, v := range s{
if temp[v] == 1 {
return k
}
}
return -1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment