Skip to content

Commit b23dab7

Browse files
committed
chore: add daily leetcode post 2299强密码检验器II_translated
1 parent 019a845 commit b23dab7

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
---
2+
title: One question daily 2299. Code inspection device II
3+
date: '2024.01.01 0:00'
4+
tags:
5+
- - Python
6+
- - answer
7+
- - Bit operation
8+
abbrlink: 7ded25bb
9+
---
10+
11+
12+
13+
Excess when executing100%User,What can I say about this question,But one**Bit operation**Knowledge point
14+
15+
# My code:
16+
17+
```python
18+
class Solution:
19+
def strongPasswordCheckerII(self, password: str) -> bool:
20+
flag1 = flag2 = flag3 = flag4 = 1
21+
ret = None
22+
for i in password:
23+
if ret == i:
24+
return False
25+
if i.isdigit():
26+
flag1 = 0
27+
elif i.isupper():
28+
flag2 = 0
29+
elif i.islower():
30+
flag3 = 0
31+
else:
32+
flag4 = 0
33+
ret = i
34+
if sum([flag1, flag2, flag3, flag4]) == 0 and len(password) >= 8:
35+
return True
36+
return False
37+
```
38+
# Bit operation代码:
39+
method one:simulation + Bit operation
40+
41+
According to the description of the topic,我们可以simulation检查密码是否满足题目要求的过程。
42+
43+
first,We check whether the length of the password is less than 8,in the case of,Then return false。
44+
45+
Next,We use a mask mask To record whether the password contains a lowercase letter、uppercase letter、Numbers and special characters。We traverse the password,Like a character every time,First determine whether it is the same as the previous character,in the case of,Then return false。Then,Update mask according to the type of character mask。at last,We check the mask mask Whether it is 15,in the case of,Then return true,否Then return false。
46+
```python
47+
class Solution:
48+
def strongPasswordCheckerII(self, password: str) -> bool:
49+
if len(password) < 8:
50+
return False
51+
mask = 0
52+
for i, c in enumerate(password):
53+
if i and c == password[i - 1]:
54+
return False
55+
if c.islower():
56+
mask |= 1
57+
elif c.isupper():
58+
mask |= 2
59+
elif c.isdigit():
60+
mask |= 4
61+
else:
62+
mask |= 8
63+
return mask == 15
64+
```
65+
66+
author:ylb
67+
Link:https://leetcode.cn/problems/strong-password-checker-ii/solutions/2068878/by-lcbin-hk2a/

0 commit comments

Comments
 (0)