?LeetCode刷題實(shí)戰(zhàn)420:強(qiáng)密碼檢驗(yàn)器

解題
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;
LeetCode1-400題匯總,希望對你有點(diǎn)幫助!
LeetCode刷題實(shí)戰(zhàn)401:二進(jìn)制手表
LeetCode刷題實(shí)戰(zhàn)402:移掉 K 位數(shù)字
LeetCode刷題實(shí)戰(zhàn)403:青蛙過河
LeetCode刷題實(shí)戰(zhàn)404:左葉子之和
LeetCode刷題實(shí)戰(zhàn)405:數(shù)字轉(zhuǎn)換為十六進(jìn)制數(shù)
LeetCode刷題實(shí)戰(zhàn)406:根據(jù)身高重建隊列
LeetCode刷題實(shí)戰(zhàn)407:接雨水 II
LeetCode刷題實(shí)戰(zhàn)408:有效單詞縮寫
LeetCode刷題實(shí)戰(zhàn)409:最長回文串
LeetCode刷題實(shí)戰(zhàn)410:分割數(shù)組的最大值
LeetCode刷題實(shí)戰(zhàn)411:最短獨(dú)占單詞縮寫
LeetCode刷題實(shí)戰(zhàn)412:Fizz Buzz
LeetCode刷題實(shí)戰(zhàn)413:等差數(shù)列劃分
LeetCode刷題實(shí)戰(zhàn)414:第三大的數(shù)
LeetCode刷題實(shí)戰(zhàn)415:字符串相加
LeetCode刷題實(shí)戰(zhàn)416:分割等和子集
LeetCode刷題實(shí)戰(zhàn)417:太平洋大西洋水流問題
LeetCode刷題實(shí)戰(zhàn)418:屏幕可顯示句子的數(shù)量
LeetCode刷題實(shí)戰(zhàn)419:甲板上的戰(zhàn)艦
