-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathtask.go
63 lines (52 loc) · 987 Bytes
/
task.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
package main
import (
"bufio"
"fmt"
"io"
"log"
"os"
"sort"
"strconv"
"strings"
"unicode"
)
func main() {
s := strings.Builder{}
Solution(os.Stdin, &s)
fmt.Println(s.String())
}
func Solution(r io.Reader, s *strings.Builder) {
scanner := bufio.NewScanner(r)
const bufCapacity = 10000000 // fix long string
buf := make([]byte, bufCapacity)
scanner.Buffer(buf, bufCapacity)
var err error
var n int
scanner.Scan()
n, err = strconv.Atoi(scanner.Text())
if err != nil {
log.Fatal(err)
}
countMap := map[string]int{}
for i := 0; i < n; i++ {
scanner.Scan()
str := scanner.Text()
if unicode.IsSpace(rune(str[len(str)-1])) {
str = str[:len(str)-1]
}
countMap[str]++
}
max := -1
var maxWords []string = nil
for k, v := range countMap {
if max == -1 || v > max {
max = v
maxWords = []string{k}
continue
} else if v == max {
maxWords = append(maxWords, k)
}
}
sort.Strings(maxWords)
s.WriteString(fmt.Sprint(maxWords[0]))
}