-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
58 lines (51 loc) · 1.34 KB
/
main.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
package main
import (
"fmt"
"strings"
"unicode"
)
func checkIfStringsFormSameSet(array []string) (bool, error) {
n := len(array)
// check if length if array is valid
if n < 1 || n > 500 {
return false, fmt.Errorf("length of array is not valid")
}
// for simplicity we will store columns in map, while validating rows
var colums = make(map[int]*strings.Builder, n)
for j := 0; j < n; j++ {
colums[j] = &strings.Builder{}
}
// check if length of each string is valid
for _, row := range array {
// We don't need to check if length of string is less than 1 or more than 500
// because we already checked length of array
// so we can check only if length of each string is not equal to the length of an array
if len(row) != n {
return false, fmt.Errorf("array is not square")
}
// check if string contains only lowercase letters
for j, char := range row {
if !unicode.IsLower(char) {
// ch is not a lowercase letter
return false, fmt.Errorf("string contains non-lowercase letter")
}
colums[j].WriteRune(char)
}
}
// iterate and check
for i, row := range array {
if row != colums[i].String() {
return false, nil
}
}
return true, nil
}
func main() {
strs := []string{"abcd", "bnrt", "crmy", "dtye"}
ok, err := checkIfStringsFormSameSet(strs)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(ok)
}