generated from projeto-de-algoritmos-2024/RepositorioTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path420_strong_password_checker.py
39 lines (33 loc) · 1.21 KB
/
420_strong_password_checker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Solution:
def strongPasswordChecker(self, password: str) -> int:
n = len(password)
has_lower = any(c.islower() for c in password)
has_upper = any(c.isupper() for c in password)
has_digit = any(c.isdigit() for c in password)
categories = int(not has_lower) + int(not has_upper) + int(not has_digit)
change = 0
one = two = 0
i = 2
while i < n:
if password[i] == password[i - 1] == password[i - 2]:
length = 2
while i < n and password[i] == password[i - 1]:
length += 1
i += 1
change += length // 3
if length % 3 == 0:
one += 1
elif length % 3 == 1:
two += 1
else:
i += 1
if n < 6:
return max(categories, 6 - n)
elif n <= 20:
return max(categories, change)
else:
delete = n - 20
change -= min(delete, one * 1) // 1
change -= min(max(delete - one, 0), two * 2) // 2
change -= max(delete - one - 2 * two, 0) // 3
return delete + max(categories, change)