-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfind_the_difference.go
67 lines (58 loc) · 1.21 KB
/
find_the_difference.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import "sync"
func findTheDifference(s string, t string) (res byte) {
originalChars, newChars := make(map[byte]int), make(map[byte]int)
stringToMap := func(str string) map[byte]int {
m := make(map[byte]int)
for i := 0; i < len(str); i++ {
if _, found := m[str[i]]; found {
m[str[i]]++
} else {
m[str[i]] = 1
}
}
return m
}
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
originalChars = stringToMap(s)
}()
go func() {
defer wg.Done()
newChars = stringToMap(t)
}()
wg.Wait()
for char, newCount := range newChars {
if originalCount, found := originalChars[char]; found {
if newCount != originalCount {
return char
}
} else {
return char
}
}
return
}
//Using XOR
// Accepted, Runtime: 0 ms, Memory Usage: 2.1 MB
// Runtime: 0 ms, faster than 100.00% of Go online submissions for Find the Difference.
// func findTheDifference(s string, t string) (res byte) {
// var wg sync.WaitGroup
// wg.Add(2)
// go func() {
// defer wg.Done()
// for i := 0; i < len(s); i++ {
// res ^= s[i]
// }
// }()
// go func() {
// defer wg.Done()
// for i := 0; i < len(t); i++ {
// res ^= t[i]
// }
// }()
// wg.Wait()
// return
// }