Created
August 16, 2017 14:32
-
-
Save luojiyin1987/9066bb58388bb8336c31ea0f7a902c23 to your computer and use it in GitHub Desktop.
First Unique Character in a String
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
| 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