-
Notifications
You must be signed in to change notification settings - Fork 40
/
length.go
94 lines (88 loc) · 2.27 KB
/
length.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package passwordvalidator
const (
seqNums = "0123456789"
seqKeyboard0 = "qwertyuiop"
seqKeyboard1 = "asdfghjkl"
seqKeyboard2 = "zxcvbnm"
seqAlphabet = "abcdefghijklmnopqrstuvwxyz"
)
func removeMoreThanTwoFromSequence(s, seq string) string {
seqRunes := []rune(seq)
runes := []rune(s)
matches := 0
for i := 0; i < len(runes); i++ {
for j := 0; j < len(seqRunes); j++ {
if i >= len(runes) {
break
}
r := runes[i]
r2 := seqRunes[j]
if r != r2 {
matches = 0
continue
}
// found a match, advance the counter
matches++
if matches > 2 {
runes = deleteRuneAt(runes, i)
} else {
i++
}
}
}
return string(runes)
}
func deleteRuneAt(runes []rune, i int) []rune {
if i >= len(runes) ||
i < 0 {
return runes
}
copy(runes[i:], runes[i+1:])
runes[len(runes)-1] = 0
runes = runes[:len(runes)-1]
return runes
}
func getReversedString(s string) string {
n := 0
rune := make([]rune, len(s))
for _, r := range s {
rune[n] = r
n++
}
rune = rune[0:n]
// Reverse
for i := 0; i < n/2; i++ {
rune[i], rune[n-1-i] = rune[n-1-i], rune[i]
}
// Convert back to UTF-8.
return string(rune)
}
func removeMoreThanTwoRepeatingChars(s string) string {
var prevPrev rune
var prev rune
runes := []rune(s)
for i := 0; i < len(runes); i++ {
r := runes[i]
if r == prev && r == prevPrev {
runes = deleteRuneAt(runes, i)
i--
}
prevPrev = prev
prev = r
}
return string(runes)
}
func getLength(password string) int {
password = removeMoreThanTwoRepeatingChars(password)
password = removeMoreThanTwoFromSequence(password, seqNums)
password = removeMoreThanTwoFromSequence(password, seqKeyboard0)
password = removeMoreThanTwoFromSequence(password, seqKeyboard1)
password = removeMoreThanTwoFromSequence(password, seqKeyboard2)
password = removeMoreThanTwoFromSequence(password, seqAlphabet)
password = removeMoreThanTwoFromSequence(password, getReversedString(seqNums))
password = removeMoreThanTwoFromSequence(password, getReversedString(seqKeyboard0))
password = removeMoreThanTwoFromSequence(password, getReversedString(seqKeyboard1))
password = removeMoreThanTwoFromSequence(password, getReversedString(seqKeyboard2))
password = removeMoreThanTwoFromSequence(password, getReversedString(seqAlphabet))
return len(password)
}