forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
73 lines (70 loc) · 1.98 KB
/
Solution.cpp
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class Solution {
public:
int strongPasswordChecker(string password) {
int types = countTypes(password);
int n = password.size();
if (n < 6) return max(6 - n, 3 - types);
if (n <= 20) {
int replace = 0, cnt = 0;
char prev = '~';
for (char& curr : password) {
if (curr == prev)
++cnt;
else {
replace += cnt / 3;
cnt = 1;
prev = curr;
}
}
replace += cnt / 3;
return max(replace, 3 - types);
}
int replace = 0, remove = n - 20;
int remove2 = 0;
int cnt = 0;
char prev = '~';
for (char& curr : password) {
if (curr == prev)
++cnt;
else {
if (remove > 0 && cnt >= 3) {
if (cnt % 3 == 0) {
--remove;
--replace;
} else if (cnt % 3 == 1)
++remove2;
}
replace += cnt / 3;
cnt = 1;
prev = curr;
}
}
if (remove > 0 && cnt >= 3) {
if (cnt % 3 == 0) {
--remove;
--replace;
} else if (cnt % 3 == 1)
++remove2;
}
replace += cnt / 3;
int use2 = min(min(replace, remove2), remove / 2);
replace -= use2;
remove -= use2 * 2;
int use3 = min(replace, remove / 3);
replace -= use3;
remove -= use3 * 3;
return (n - 20) + max(replace, 3 - types);
}
int countTypes(string& s) {
int a = 0, b = 0, c = 0;
for (char& ch : s) {
if (islower(ch))
a = 1;
else if (isupper(ch))
b = 1;
else if (isdigit(ch))
c = 1;
}
return a + b + c;
}
};