Участник:Danillich/StrongPasswordChecker

Материал из DISCOPAL
Перейти к: навигация, поиск

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

class Solution:
    def strongPasswordChecker(self, s: str) -> int:
        upper = False
        lower = False
        digit = False
        l = len(s)
 
        r_list = []
        repeat = 0
        ch_repeat = ''
        for ch in s:
            if ch.isupper() and not ch.isdigit():
                upper = True
            elif ch.isdigit():
                digit = True
            else:
                lower = True
 
            if ch_repeat == ch:
                repeat += 1
            else:
                if repeat < 3:
                    ch_repeat = ch
                    repeat = 1
                else:
                    r_list.append(repeat)
                    ch_repeat = ch
                    repeat = 1
 
        if repeat >= 3:
            r_list.append(repeat)
 
        changes = 0
        if (l >= 6) and (l <= 20): 
            for r in r_list:
                changes += r//3
            changes = max(changes, 3 - (upper + lower + digit))
        if (l < 6):
            for r in r_list:
                changes += r//3
            changes = max(changes, 3 - (upper + lower + digit))
            if changes < 6 - l:
                changes = 6 - l
        if (l > 20):
            deletions = 0
            stop_flag = False
            # r%3==0
            for i, r in enumerate(r_list):
                if r%3 == 0:
                    deletions += 1
                    r_list[i] -= 1
                    l -= 1
                    changes += 1
                    if l <= 20:
                        stop_flag = True
 
            # r%3==1
            if not stop_flag:
                for i, r in enumerate(r_list):
                    if r%3 == 1:
                        deletions += 2
                        r_list[i] -= 2
                        l -= 2
                        changes += 2
                        if l <= 20:
                            stop_flag = True
 
            # r%3==2
            if not stop_flag:
                for i, r in enumerate(r_list):
                    if r%3 == 2:
                        if l - 20 >= 3:
                            r_list[i] -= 3
                            deletions += 3
                            l -= 3
                            changes += 3
                        else:
                            r_list[i] -= l - 20
                            deletions += l - 20
                            changes += l - 20
                            l -= l - 20
 
            for r in r_list:
                changes += r//3
 
            replaces = changes - deletions
            changes += max(0, 3 - (upper + lower + digit) - replaces)
 
            if (l > 20):
                changes += l - 20
 
        return changes