2299. Strong Password Checker II Easy
1/**
2 * [2299] Strong Password Checker II
3 *
4 * A password is said to be strong if it satisfies all the following criteria:
5 *
6 * It has at least 8 characters.
7 * It contains at least one lowercase letter.
8 * It contains at least one uppercase letter.
9 * It contains at least one digit.
10 * It contains at least one special character. The special characters are the characters in the following string: "!@#$%^&*()-+".
11 * It does not contain 2 of the same character in adjacent positions (i.e., "aab" violates this condition, but "aba" does not).
12 *
13 * Given a string password, return true if it is a strong password. Otherwise, return false.
14 *
15 * Example 1:
16 *
17 * Input: password = "IloveLe3tcode!"
18 * Output: true
19 * Explanation: The password meets all the requirements. Therefore, we return true.
20 *
21 * Example 2:
22 *
23 * Input: password = "Me+You--IsMyDream"
24 * Output: false
25 * Explanation: The password does not contain a digit and also contains 2 of the same character in adjacent positions. Therefore, we return false.
26 *
27 * Example 3:
28 *
29 * Input: password = "1aB!"
30 * Output: false
31 * Explanation: The password does not meet the length requirement. Therefore, we return false.
32 *
33 * Constraints:
34 *
35 * 1 <= password.length <= 100
36 * password consists of letters, digits, and special characters: "!@#$%^&*()-+".
37 *
38 */
39pub struct Solution {}
40
41// problem: https://leetcode.com/problems/strong-password-checker-ii/
42// discuss: https://leetcode.com/problems/strong-password-checker-ii/discuss/?currentPage=1&orderBy=most_votes&query=
43
44// submission codes start here
45
46impl Solution {
47 pub fn strong_password_checker_ii(password: String) -> bool {
48 if password.len() < 8 {
49 return false;
50 }
51 let (mut l, mut u, mut d, mut s) = (false, false, false, false);
52 let mut pre = ' ';
53 for c in password.chars() {
54 if c == pre {
55 return false;
56 }
57
58 if c.is_lowercase() {
59 l = true;
60 } else if c.is_uppercase() {
61 u = true;
62 } else if c.is_digit(10) {
63 d = true;
64 } else {
65 s = true;
66 }
67 pre = c;
68 }
69 l && u && d && s
70 }
71}
72
73// submission codes end
74
75#[cfg(test)]
76mod tests {
77 use super::*;
78
79 #[test]
80 fn test_2299() {
81 assert!(Solution::strong_password_checker_ii("IloveLe3tcode!".to_string()));
82 }
83}
84
Back
© 2025 bowen.ge All Rights Reserved.