Cho chuỗi password, kiểm tra các tiêu chí:
- Độ dài 8-20 ký tự.
- Ít nhất 1 chữ số, 1 chữ thường, 1 chữ hoa, 1 ký tự đặc biệt (
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~). - Không chứa ký tự khác (tab, newline...).
Ví dụ
Đầu vào: "@bCd12#4".
Đầu ra: True.
Thuật toán
- Khởi tạo bộ đếm
a,b,c,dcho chữ hoa, thường, đặc biệt, số. - Kiểm tra độ dài.
- Duyệt từng ký tự, tăng bộ đếm tương ứng.
- Trả về
Truenếu tất cả bộ đếm >=1 và tổng bằng độ dài chuỗi.
Mã nguồn Python
class Solution:
def solve(self, password):
a=b=c=d=0
if len(password)<8 or len(password)>20:
return False
for i in password:
if i.isupper(): a+=1
elif i.islower(): b+=1
elif i in '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~': c+=1
elif i.isdigit(): d+=1
if a>=1 and b>=1 and c>=1 and d>=1 and a+b+c+d==len(password):
return True
else:
return False
s = "@bCd12#4"
ob = Solution()
print(ob.solve(s))
Đầu vào / Đầu ra
Input: "@bCd12#4"
Output: True