LeetCode刷题实战420:强密码检验器
解题
class Solution:
def strongPasswordChecker(self, s: str) -> int:
cnt = [0]*3 # 3n, 3n+1, 3n+2
a , A, num = 1, 1, 1
i = 0
n_modify = 0
while i < len(s):
c = s[i]
length = 1
if s[i] >= '0' and s[i] <= '9':
num = 0
elif s[i] >= 'a' and s[i] <= 'z':
a = 0
elif s[i] >= 'A' and s[i] <= 'Z':
A = 0
i += 1
while i < len(s) and s[i] == c:
i += 1
length += 1
if length >= 3:
n_modify += length//3
cnt[length%3] += 1
n_lack = a + A + num
if len(s) < 6:
return max(n_lack, 6-len(s))
if len(s) <= 20:
return max(n_lack, n_modify)
n_delete = len(s) - 20
if n_delete <= cnt[0]:
return max(n_modify - n_delete, n_lack) + n_delete
if (n_delete - cnt[0]) <= 2*cnt[1]:
return max(n_modify - cnt[0]- (n_delete-cnt[0])//2, n_lack) + n_delete
if (n_delete - cnt[0] - 2*cnt[1]) <= 3 * cnt[2]:
return max(n_modify - cnt[0] - cnt[1]- (n_delete - cnt[0] - 2*cnt[1])//3, n_lack) + n_delete;
return max(n_modify - cnt[0] - cnt[1] - cnt[2] - (n_delete - cnt[0] - 2*cnt[1]
-3*cnt[2])//3, n_lack) + n_delete;