-
Notifications
You must be signed in to change notification settings - Fork 1
/
is_valid.go
73 lines (58 loc) · 1.06 KB
/
is_valid.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
68
69
70
71
72
73
package main
import (
"bufio"
"fmt"
"os"
)
func isValid(s string) string {
counter := countLetters(s)
var max int
var removed bool
for _, occurrence := range counter {
if occurrence == 0 || occurrence == max {
continue
}
if max == 0 {
max = occurrence
continue
}
if !removed && (occurrence == max+1 || occurrence == 1) {
removed = true
continue
}
return "NO"
}
return "YES"
}
func countLetters(s string) []int {
counts := make([]int, 26)
for _, r := range s {
counts[r-'a']++
}
return counts
}
func main() {
stdin, err := os.Open(os.Getenv("INPUT_PATH"))
if err != nil {
stdin = os.Stdin
}
defer stdin.Close()
stdout, err := os.Create(os.Getenv("OUTPUT_PATH"))
if err != nil {
stdout = os.Stdout
}
defer stdout.Close()
reader := bufio.NewReaderSize(stdin, 1024*1024)
writer := bufio.NewWriterSize(stdout, 1024*1024)
var s string
_, err = fmt.Fscan(reader, &s)
checkError(err)
result := isValid(s)
fmt.Fprintln(writer, result)
writer.Flush()
}
func checkError(err error) {
if err != nil {
panic(err)
}
}