Last active
August 6, 2017 08:34
-
-
Save luojiyin1987/a62b0f1bb098e02c3fcd0036b689845b to your computer and use it in GitHub Desktop.
Find the Difference
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 findTheDifference(s string, t string) byte { | |
| var ans int32 | |
| ans = 0 | |
| for _, cs := range s { | |
| ans ^= cs | |
| } | |
| for _, ct := range t { | |
| ans ^= ct | |
| } | |
| return byte(ans) | |
| } | |
| func findTheDifference(s string, t string) byte { | |
| //var r byte | |
| /* | |
| countS := make(map[byte]int, 0) | |
| for _, v := range []byte(s){ | |
| countS[v]++ | |
| } | |
| countT := make(map[byte]int, 0) | |
| for _, v := range []byte(t){ | |
| countT[v]++ | |
| } | |
| for k,v := range countT { | |
| _, ok := countS[k] | |
| if !ok { | |
| return k | |
| } | |
| if v != countS[k]{ | |
| return k | |
| } | |
| } | |
| return r | |
| */ | |
| u := []byte(s + t) | |
| d := 0 | |
| for _,v := range u { | |
| d ^= int(v) | |
| } | |
| return byte(d) | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment