forked from csujedihy/lc-all-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrong-password-checker.py
43 lines (40 loc) · 1.31 KB
/
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
40
41
42
43
class Solution(object):
def strongPasswordChecker(self, s):
"""
:type s: str
:rtype: int
"""
complexBal = 3
if any(c in string.lowercase for c in s):
complexBal -= 1
if any(c in string.uppercase for c in s):
complexBal -= 1
if any(c.isdigit() for c in s):
complexBal -= 1
one = 0
two = 0
p = 2
replace = 0
while p < len(s):
if s[p] == s[p-1] == s[p-2]:
length = 2
while p < len(s) and s[p] == s[p-1]:
p += 1
length += 1
replace += length / 3
if length % 3 == 0:
one += 1
if length % 3 == 1:
two += 1
else:
p += 1
if len(s) < 6:
return max(complexBal, 6 - len(s))
elif len(s) <= 20:
return max(complexBal, replace)
else:
redundant = len(s) - 20
replace -= min(redundant, one)
replace -= min(max(redundant - one, 0), two * 2) / 2
replace -= max(redundant - one - two * 2, 0) / 3
return redundant + max(complexBal, replace)