-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.h
99 lines (90 loc) · 2.52 KB
/
solution.h
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
Code generated by https://github.com/goodstudyqaq/leetcode-local-tester
*/
#if __has_include("../utils/cpp/help.hpp")
#include "../utils/cpp/help.hpp"
#elif __has_include("../../utils/cpp/help.hpp")
#include "../../utils/cpp/help.hpp"
#else
#define debug(...) 42
#endif
class Solution {
public:
bool is_digit(char it) {
return it >= '0' && it <= '9';
}
bool is_small(char it) {
return it >= 'a' && it <= 'z';
}
bool is_big(char it) {
return it >= 'A' && it <= 'Z';
}
int strongPasswordChecker(string password) {
int n = password.size();
debug(n);
if (n <= 3) {
return 6 - n;
}
typedef pair<char, int> pci;
vector<pci> V;
int now = 0;
int delete_num = 0;
if (n > 20) delete_num = n - 20;
int ans = delete_num;
// delete
while (now < n) {
int go = now;
while (go < n && password[go] == password[now]) go++;
int len = go - now;
V.push_back({password[now], len});
now = go;
}
while (delete_num) {
int idx = -1;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < V.size(); j++) {
if (V[j].second >= 3 && V[j].second % 3 == i) {
idx = j;
i = 10;
break;
}
}
}
if (idx == -1) break;
delete_num--;
V[idx].second--;
}
vector<bool> flag(3);
for (int i = 0; i < n; i++) {
if (is_digit(password[i])) flag[0] = 1;
if (is_small(password[i])) flag[1] = 1;
if (is_big(password[i])) flag[2] = 1;
}
int need = 3 - flag[0] - flag[1] - flag[2];
debug(need);
int add = 0;
if (n < 6) add = 6 - n;
ans += add;
need -= min(need, add);
debug(ans, add);
// modify
int modify_num = 0;
debug(V);
for (int i = 0; i < V.size(); i++) {
if (V[i].second >= 3) {
int tmp = V[i].second / 3;
if (add > 0) {
debug(tmp, add);
int tmp2 = min(tmp, add);
tmp -= tmp2;
add -= tmp2;
}
modify_num += tmp;
}
}
need -= min(need, modify_num);
ans += modify_num;
ans += need;
return ans;
}
};