-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathransom-note.go
62 lines (54 loc) · 947 Bytes
/
ransom-note.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
package main
import "fmt"
func main() {
var first int
if m, err := fmt.Scan(&first); m != 1 {
panic(err)
}
var second int
if m, err := fmt.Scan(&second); m != 1 {
panic(err)
}
fWords := createMaps(first)
sWords := createMaps(second)
ransom := false
for k, v := range sWords.found {
if _, ok := fWords.found[k]; !ok {
ransom = false
break
} else if (v > fWords.found[k]) {
ransom = false
break
} else {
ransom = true
}
}
if ransom {
fmt.Println("Yes")
} else {
fmt.Println("No")
}
}
type words struct {
found map[string]int
}
func newWords() *words {
return &words{found:map[string]int{}}
}
func (w *words) add(word string, add int) {
count, ok := w.found[word]
if !ok {
w.found[word] = add
return
}
w.found[word] = count + add
}
func createMaps(len int) *words {
dict := newWords()
for i := 0; i < len; i++ {
var word string
fmt.Scan(&word)
dict.add(word, 1)
}
return dict
}