😕题目描述

https://leetcode-cn.com/problems/strong-password-checker/

😔分析

对于该问题需要进行分类讨论,首选针对字符串的长度进行分类:

  • 420 强密码检验器 - 图1:此时无需只需判定长度要求和类型要求,取大值。例如:
    • aaaaa此时如果检查长度需要补齐1位;
    • 如果检查类型需要添加两位,所以最终结果是两位
  • 420 强密码检验器 - 图2:此时只需要判定类型要求和连续长度要求,取两者的较大值。
  • 420 强密码检验器 - 图3:此时必须要进行的工作就是删除,所以删除的步骤是固定值,但是替换可以在删除之后再进行,但是这个替换个数受到删除个数的影响。比如:aaaaaabbbbbccccc假设需要删除的个数是6个,

    • 其中字符串变成了6个a、5个b、4个c,需要替换的总数的是2+1+1个
      • 如果是删除一个a那么此时相当于替换了一个a;
      • 如果是删除两个b那么就相当于替换了一个b;
      • 如果删除三个c那么相当于替换了一个c。
    • 经过删除后替换的个数就降低到了6-1-1-1=3个,此时替换数目大于类型检查需要替换的个数,所以最终结果就是删除数量+3。如果最终的替换数目小于类型检查需要替换的个数,那么就是删除数量+类型检查数量。 :::info 对于数量大于等于三的字符个数**n**进行切割:
  • n%3==0:420 强密码检验器 - 图4可以进行1次,420 强密码检验器 - 图5可以进行n/3-1次;

  • n%3==1:420 强密码检验器 - 图6可以进行1次,420 强密码检验器 - 图7可以进行n/3-1次;
  • n%3==2:420 强密码检验器 - 图8可以进行n/3-1次。 ::: :::tips 初始替换次数为**k**:

  • 消耗420 强密码检验器 - 图9n1个,此时k=k-n1;

  • 消耗420 强密码检验器 - 图10n2个,此时k=k-n2/2;
  • 消耗420 强密码检验器 - 图11n3个,此时k=k-n3/3;

消耗完成后如果还剩有替换次数:

  • 如果剩余替换次数>类型检查次数:结果为420 强密码检验器 - 图12;
  • 如果剩余替换次数<类型检查次数:结果为420 强密码检验器 - 图13。 :::

    😏答案

    ```go func strongPasswordChecker(password string) int { if len(password) > 20 {
    1. return stringSize20(password)
    } fmt.Printf(“continue: %d\n type: %d\n”, checkContinuity(password), checkType(password)) return getMax420(checkContinuity(password), checkLength(password), checkType(password)) }

func checkLength(password string) int { if len(password) < 6 { return 6 - len(password) } if len(password) > 20 { return 20 - len(password) }

return 0

}

func stringSize20(password string) int { count1 := 0 count2 := 0 count3 := 0

count := 0
flag := ' '

for _, v := range password {
    if v == flag {
        count++
    } else {
        if count >= 3 {
            switch count % 3 {
            case 0:
                count1++
                count3 += count/3 - 1
            case 1:
                count2++
                count3 += count/3 - 1
            case 2:
                count3 += (count + 1) / 3
            }
        }
        flag = v
        count = 1
    }
}

if count >= 3 {
    switch count % 3 {
    case 0:
        count1++
        count3 += count/3 - 1
    case 1:
        count2++
        count3 += count/3 - 1
    case 2:
        count3 += (count + 1) / 3
    }
}

replace := getMax420(checkContinuity(password), checkType(password), 0)

fmt.Printf("replace: %d\n", replace)
n := len(password) - 20
if n != 0 {
    if n > count1 {
        n = n - count1
        replace = replace - count1
    } else {
        replace = replace - n
        n = 0
    }
}
fmt.Printf("replace: %d\n", replace)

if n != 0 {
    if n > count2*2 {
        n = n - count2*2
        replace -= count2
    } else {
        replace -= n / 2
        n = 0

    }
}
fmt.Printf("replace: %d\n", replace)
if n != 0 {
    if n > count3*3 {
        replace -= count3
    } else {
        replace -= n / 3
    }
}
fmt.Printf("replace: %d\n", replace)

fmt.Printf("count3: %d\ncount2: %d\n count1: %d", count3, count2, count1)

if replace < checkType(password) {
    return len(password) - 20 + checkType(password)
}
return len(password) - 20 + replace

}

func checkType(password string) int { countNum := 0 countUp := 0 countLow := 0

for _, v := range password {
    if v >= '0' && v <= '9' {
        countNum++
    }

    if v >= 'a' && v <= 'z' {
        countLow++
    }

    if v >= 'A' && v <= 'Z' {
        countUp++
    }
}

res := 0
if countUp == 0 {
    res++
}
if countNum == 0 {
    res++
}
if countLow == 0 {
    res++
}
return res

}

func checkContinuity(password string) int { sum := 0 count := 0 flag := ‘ ‘

for _, v := range password {
    if v == flag {
        count++
    } else {
        flag = v
        sum += (count / 3)
        count = 1
    }
}
sum += count / 3
return sum

}

func getMax420(l1, l2, l3 int) int { if l1 >= l2 && l1 >= l3 { return l1 }

if l2 >= l1 && l2 >= l3 {
    return l2
}

if l3 >= l1 && l3 >= l2 {
    return l3
}

return l1

} ```