Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f7f6ae9

Browse files
committed
feat: add solutions to lc problems
* No.0420.Strong Password Checker * No.0582.Kill Process
1 parent a136e22 commit f7f6ae9

File tree

11 files changed

+895
-3
lines changed

11 files changed

+895
-3
lines changed

‎solution/0400-0499/0420.Strong Password Checker/README.md‎

Lines changed: 221 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,235 @@
6868
<!-- 这里可写当前语言的特殊实现逻辑 -->
6969

7070
```python
71-
71+
class Solution:
72+
def strongPasswordChecker(self, password: str) -> int:
73+
def countTypes(s):
74+
a = b = c = 0
75+
for ch in s:
76+
if ch.islower():
77+
a = 1
78+
elif ch.isupper():
79+
b = 1
80+
elif ch.isdigit():
81+
c = 1
82+
return a + b + c
83+
84+
types = countTypes(password)
85+
n = len(password)
86+
if n < 6:
87+
return max(6 - n, 3 - types)
88+
if n <= 20:
89+
replace = cnt = 0
90+
prev = '~'
91+
for curr in password:
92+
if curr == prev:
93+
cnt += 1
94+
else:
95+
replace += cnt // 3
96+
cnt = 1
97+
prev = curr
98+
replace += cnt // 3
99+
return max(replace, 3 - types)
100+
replace = cnt = 0
101+
remove, remove2 = n - 20, 0
102+
prev = '~'
103+
for curr in password:
104+
if curr == prev:
105+
cnt += 1
106+
else:
107+
if remove > 0 and cnt >= 3:
108+
if cnt % 3 == 0:
109+
remove -= 1
110+
replace -= 1
111+
elif cnt % 3 == 1:
112+
remove2 += 1
113+
replace += cnt // 3
114+
cnt = 1
115+
prev = curr
116+
if remove > 0 and cnt >= 3:
117+
if cnt % 3 == 0:
118+
remove -= 1
119+
replace -= 1
120+
elif cnt % 3 == 1:
121+
remove2 += 1
122+
replace += cnt // 3
123+
use2 = min(replace, remove2, remove // 2)
124+
replace -= use2
125+
remove -= use2 * 2
126+
127+
use3 = min(replace, remove // 3)
128+
replace -= use3
129+
remove -= use3 * 3
130+
return n - 20 + max(replace, 3 - types)
72131
```
73132

74133
### **Java**
75134

76135
<!-- 这里可写当前语言的特殊实现逻辑 -->
77136

78137
```java
138+
class Solution {
139+
public int strongPasswordChecker(String password) {
140+
int types = countTypes(password);
141+
int n = password.length();
142+
if (n < 6) {
143+
return Math.max(6 - n, 3 - types);
144+
}
145+
char[] chars = password.toCharArray();
146+
if (n <= 20) {
147+
int replace = 0;
148+
int cnt = 0;
149+
char prev = '~';
150+
for (char curr : chars) {
151+
if (curr == prev) {
152+
++cnt;
153+
} else {
154+
replace += cnt / 3;
155+
cnt = 1;
156+
prev = curr;
157+
}
158+
}
159+
replace += cnt / 3;
160+
return Math.max(replace, 3 - types);
161+
}
162+
int replace = 0, remove = n - 20;
163+
int remove2 = 0;
164+
int cnt = 0;
165+
char prev = '~';
166+
for (char curr : chars) {
167+
if (curr == prev) {
168+
++cnt;
169+
} else {
170+
if (remove > 0 && cnt >= 3) {
171+
if (cnt % 3 == 0) {
172+
--remove;
173+
--replace;
174+
} else if (cnt % 3 == 1) {
175+
++remove2;
176+
}
177+
}
178+
replace += cnt / 3;
179+
cnt = 1;
180+
prev = curr;
181+
}
182+
}
183+
if (remove > 0 && cnt >= 3) {
184+
if (cnt % 3 == 0) {
185+
--remove;
186+
--replace;
187+
} else if (cnt % 3 == 1) {
188+
++remove2;
189+
}
190+
}
191+
replace += cnt / 3;
192+
193+
int use2 = Math.min(Math.min(replace, remove2), remove / 2);
194+
replace -= use2;
195+
remove -= use2 * 2;
196+
197+
int use3 = Math.min(replace, remove / 3);
198+
replace -= use3;
199+
remove -= use3 * 3;
200+
return (n - 20) + Math.max(replace, 3 - types);
201+
}
202+
203+
private int countTypes(String s) {
204+
int a = 0, b = 0, c = 0;
205+
for (char ch : s.toCharArray()) {
206+
if (Character.isLowerCase(ch)) {
207+
a = 1;
208+
} else if (Character.isUpperCase(ch)) {
209+
b = 1;
210+
} else if (Character.isDigit(ch)) {
211+
c = 1;
212+
}
213+
}
214+
return a + b + c;
215+
}
216+
}
217+
```
79218

219+
### **C++**
220+
221+
```cpp
222+
class Solution {
223+
public:
224+
int strongPasswordChecker(string password) {
225+
int types = countTypes(password);
226+
int n = password.size();
227+
if (n < 6) return max(6 - n, 3 - types);
228+
if (n <= 20)
229+
{
230+
int replace = 0, cnt = 0;
231+
char prev = '~';
232+
for (char& curr : password)
233+
{
234+
if (curr == prev) ++cnt;
235+
else
236+
{
237+
replace += cnt / 3;
238+
cnt = 1;
239+
prev = curr;
240+
}
241+
}
242+
replace += cnt / 3;
243+
return max(replace, 3 - types);
244+
}
245+
int replace = 0, remove = n - 20;
246+
int remove2 = 0;
247+
int cnt = 0;
248+
char prev = '~';
249+
for (char& curr : password)
250+
{
251+
if (curr == prev) ++cnt;
252+
else
253+
{
254+
if (remove > 0 && cnt >= 3)
255+
{
256+
if (cnt % 3 == 0)
257+
{
258+
--remove;
259+
--replace;
260+
}
261+
else if (cnt % 3 == 1) ++remove2;
262+
}
263+
replace += cnt / 3;
264+
cnt = 1;
265+
prev = curr;
266+
}
267+
}
268+
if (remove > 0 && cnt >= 3)
269+
{
270+
if (cnt % 3 == 0)
271+
{
272+
--remove;
273+
--replace;
274+
}
275+
else if (cnt % 3 == 1) ++remove2;
276+
}
277+
replace += cnt / 3;
278+
279+
int use2 = min(min(replace, remove2), remove / 2);
280+
replace -= use2;
281+
remove -= use2 * 2;
282+
283+
int use3 = min(replace, remove / 3);
284+
replace -= use3;
285+
remove -= use3 * 3;
286+
return (n - 20) + max(replace, 3 - types);
287+
}
288+
289+
int countTypes(string& s) {
290+
int a = 0, b = 0, c = 0;
291+
for (char& ch : s)
292+
{
293+
if (islower(ch)) a = 1;
294+
else if (isupper(ch)) b = 1;
295+
else if (isdigit(ch)) c = 1;
296+
}
297+
return a + b + c;
298+
}
299+
};
80300
```
81301
82302
### **...**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /