func strongPasswordChecker(s string) int { // Missing: insert/delete/replace // Too short: insert // Too long: delete // Repeat: insert/delete/replace n := len(s) if n == 0 { return 6 } a, A, d, cnt := 1, 1, 1, 0 // Find the cnt of missing and repeat repeat := make([]int, 0) pre := rune(s[0]) for _, r := range s { if 'a' <= r && r <= 'z' { a = 0 } else if 'A' <= r && r <= 'Z' { A = 0 } else if '0' <= r && r <= '9' { d = 0 } if r == pre { cnt++ } else { if 3 <= cnt { repeat = append(repeat, cnt) } cnt = 1 pre = r } } if 3 <= cnt { repeat = append(repeat, cnt) } miss := a + A + d 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-- { 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 } } for i := 0; i < m; i++ { } return 0 } func minInt(x, y int) int { if x < y { return x } return y } func maxInt(x, y int) int { if x < y { return y } return x }