Created
September 24, 2018 07:10
-
-
Save namtx/204a517043b8a77a333207ed4521fdad to your computer and use it in GitHub Desktop.
First Unique Character in a String (https://leetcode.com/problems/first-unique-character-in-a-string/description/)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
func main() { | |
s := "leetcode" | |
fmt.Println(firstUniqChar(s)) | |
} | |
func firstUniqChar(s string) int { | |
runes := []rune(s) | |
check := map[rune]int{} | |
for _, rune := range runes { | |
check[rune]++ | |
} | |
for i, rune := range runes { | |
if check[rune] == 1 { | |
return i | |
} | |
} | |
return -1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment