|
|
@@ -35,20 +35,27 @@ func strongPasswordChecker(s string) int {
|
|
|
if n < 6 {
|
|
|
return miss + maxInt(0, 6-n-miss) // Repeat && missing can be both solved by insert
|
|
|
}
|
|
|
- exceed, m := maxInt(0, n-20), len(repeat)
|
|
|
- for k := 2; 1 <= k; k-- {
|
|
|
+ exceed, replace, m := maxInt(0, n-20), 0, len(repeat)
|
|
|
+ res := exceed
|
|
|
+ for k := 1; k <= 2; k++ { // Smaller k first, cauz aaa -> aa: 1 step, aaaa -> xaa: 2 step
|
|
|
for i := 0; i < m && 0 < exceed; i++ {
|
|
|
if repeat[i] < 3 || repeat[i]%3 != k-1 {
|
|
|
continue
|
|
|
}
|
|
|
- repeat[i] -= minInt(exceed, k)
|
|
|
- exceed -= k
|
|
|
+ dec := minInt(exceed, k)
|
|
|
+ repeat[i], exceed = repeat[i]-dec, exceed-dec
|
|
|
}
|
|
|
}
|
|
|
for i := 0; i < m; i++ {
|
|
|
-
|
|
|
+ if 3 <= repeat[i] && 0 < exceed {
|
|
|
+ dec := minInt(exceed, repeat[i]-2) // 3k+2 -> 2 to make exceed == 0
|
|
|
+ repeat[i], exceed = repeat[i]-dec, exceed-dec
|
|
|
+ }
|
|
|
+ if 3 <= repeat[i] {
|
|
|
+ replace += repeat[i] / 3
|
|
|
+ }
|
|
|
}
|
|
|
- return 0
|
|
|
+ return res + maxInt(miss, replace)
|
|
|
}
|
|
|
|
|
|
func minInt(x, y int) int {
|
|
|
@@ -64,4 +71,3 @@ func maxInt(x, y int) int {
|
|
|
}
|
|
|
return x
|
|
|
}
|
|
|
-
|