Dễ 4 người đã giải Tỷ lệ chấp nhận 57.1%

Password Rules Check

Check a password against simple strength rules.

Bạn sẽ học được gì

  • Check several rules on one string
  • Use flags for digits and letters
  • Return a clear true or false result

Đề bài

Xem bản dịch

A password is strong if it has at least 8 characters, at least one uppercase letter, at least one lowercase letter and at least one digit (English letters and 0–9).

Implement is_strong_password(password).

Input

password: The password to check (ASCII characters).

Output

Return true if all rules hold, otherwise false.

Ràng buộc

0 ≤ length of password ≤ 100

Ví dụ

Ví dụ 1
Đầu vào:
password = "Secret123"

Kết quả mong đợi:
expected = True
Đầu vào:
std::string password = "Secret123";

Kết quả mong đợi:
bool expected = true;
Đầu vào:
string password = "Secret123";

Kết quả mong đợi:
bool expected = true;

Giải thích

9 characters with uppercase S, lowercase letters and digits: strong.

Ví dụ 2
Đầu vào:
password = "password1"

Kết quả mong đợi:
expected = False
Đầu vào:
std::string password = "password1";

Kết quả mong đợi:
bool expected = false;
Đầu vào:
string password = "password1";

Kết quả mong đợi:
bool expected = false;

Giải thích

There is no uppercase letter, so it is not strong.

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

The password must satisfy several independent conditions at once.

Gợi ý 2

Track whether you have seen an uppercase letter, a lowercase letter, and a digit while checking length.

Gợi ý 3

An empty string or short string fails immediately even if it contains mixed characters.

Độ phức tạp mong đợi

O(n) time, O(1) extra space

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Lời giải đạt tốt nhất của mỗi người. Lời giải nhanh hơn, tốn ít bộ nhớ hơn và được đánh giá cao hơn sẽ đứng trên.

# Người giải Ngôn ngữ Thời gian Bộ nhớ AI đánh giá Lời giải
1 LMLaura Mahlow Python 932 ms - 4/5 Xem code
2 HJhettory Jurash Python 957 ms - 4/5 Xem code
3 RCRiccardo Corti Python 966 ms - 4/5 Xem code
4 ゼゼロダニ Java 3,552 ms - 5/5 Xem code

Học hỏi từ người khác

Lời giải cộng đồng

Cảnh báo: Có 4 lời giải đầy đủ bên dưới Hãy thử tự giải trước. Bạn vẫn có thể xem code của người khác và nhận xét của AI. Đăng nhập và giải thử thách để tham gia thảo luận.