|
| 1 | +--- |
| 2 | +id: Strong-Password-Checker-II |
| 3 | +title: Strong Password Checker II |
| 4 | +sidebar_label: 2299-Strong Password Checker II |
| 5 | +tags: |
| 6 | + - Arrays |
| 7 | + - C++ |
| 8 | + - Java |
| 9 | + - Python |
| 10 | +description: "This document provides solutions to this problem implemented in C++, Java, and Python." |
| 11 | +--- |
| 12 | + |
| 13 | +## Problem |
| 14 | + |
| 15 | +A password is said to be strong if it satisfies all the following criteria: |
| 16 | + |
| 17 | +It has at least 8 characters. |
| 18 | +It contains at least one lowercase letter. |
| 19 | +It contains at least one uppercase letter. |
| 20 | +It contains at least one digit. |
| 21 | +It contains at least one special character. The special characters are the characters in the following string: "!@#$%^&*()-+". |
| 22 | +It does not contain 2 of the same character in adjacent positions (i.e., "aab" violates this condition, but "aba" does not). |
| 23 | +Given a string password, return true if it is a strong password. Otherwise, return false. |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +### Examples |
| 28 | + |
| 29 | +**Example 1:** |
| 30 | + |
| 31 | +Input: password = "IloveLe3tcode!" |
| 32 | +Output: true |
| 33 | +Explanation: The password meets all the requirements. Therefore, we return true. |
| 34 | + |
| 35 | +**Example 2:** |
| 36 | + |
| 37 | +Input: password = "Me+You--IsMyDream" |
| 38 | +Output: false |
| 39 | +Explanation: The password does not contain a digit and also contains 2 of the same character in adjacent positions. Therefore, we return false. |
| 40 | + |
| 41 | +**Example 3"** |
| 42 | + |
| 43 | +Input: password = "1aB!" |
| 44 | +Output: false |
| 45 | +Explanation: The password does not meet the length requirement. Therefore, we return false. |
| 46 | + |
| 47 | +### Constraints |
| 48 | + |
| 49 | +- `1 <= password.length <= 100` |
| 50 | +- `password consists of letters, digits, and special characters: "!@#$%^&*()-+".` |
| 51 | + |
| 52 | +### Approach |
| 53 | + |
| 54 | +Length Check: Ensure the password has at least 8 characters. |
| 55 | +Character Type Checks: Verify that the password contains: |
| 56 | +- At least one lowercase letter. |
| 57 | +- At least one uppercase letter. |
| 58 | +- At least one digit. |
| 59 | +- At least one special character from "!@#$%^&*()-+". |
| 60 | +Adjacent Duplicate Check: Ensure no two identical characters are adjacent. |
| 61 | +### Solution |
| 62 | + |
| 63 | +#### Code in Different Languages |
| 64 | + |
| 65 | +### C++ Solution |
| 66 | + |
| 67 | +```cpp |
| 68 | +#include <string> |
| 69 | +using namespace std; |
| 70 | + |
| 71 | +bool isStrongPassword(const string& password) { |
| 72 | + if (password.size() < 8) return false; |
| 73 | + |
| 74 | + bool hasLower = false, hasUpper = false, hasDigit = false, hasSpecial = false; |
| 75 | + string specialChars = "!@#$%^&*()-+"; |
| 76 | + |
| 77 | + for (size_t i = 0; i < password.size(); ++i) { |
| 78 | + if (islower(password[i])) hasLower = true; |
| 79 | + if (isupper(password[i])) hasUpper = true; |
| 80 | + if (isdigit(password[i])) hasDigit = true; |
| 81 | + if (specialChars.find(password[i]) != string::npos) hasSpecial = true; |
| 82 | + if (i > 0 && password[i] == password[i - 1]) return false; |
| 83 | + } |
| 84 | + |
| 85 | + return hasLower && hasUpper && hasDigit && hasSpecial; |
| 86 | +} |
| 87 | + |
| 88 | +// Example usage: |
| 89 | +#include <iostream> |
| 90 | +int main() { |
| 91 | + string password1 = "IloveLe3tcode!"; |
| 92 | + cout << isStrongPassword(password1) << endl; // Output: 1 (true) |
| 93 | + |
| 94 | + string password2 = "Me+You--IsMyDream"; |
| 95 | + cout << isStrongPassword(password2) << endl; // Output: 0 (false) |
| 96 | + |
| 97 | + string password3 = "1aB!"; |
| 98 | + cout << isStrongPassword(password3) << endl; // Output: 0 (false) |
| 99 | + |
| 100 | + return 0; |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | +``` |
| 106 | +
|
| 107 | +### Java Solution |
| 108 | +
|
| 109 | +```java |
| 110 | +public class StrongPasswordChecker { |
| 111 | + public static boolean isStrongPassword(String password) { |
| 112 | + if (password.length() < 8) return false; |
| 113 | +
|
| 114 | + boolean hasLower = false, hasUpper = false, hasDigit = false, hasSpecial = false; |
| 115 | + String specialChars = "!@#$%^&*()-+"; |
| 116 | +
|
| 117 | + for (int i = 0; i < password.length(); i++) { |
| 118 | + char ch = password.charAt(i); |
| 119 | + if (Character.isLowerCase(ch)) hasLower = true; |
| 120 | + if (Character.isUpperCase(ch)) hasUpper = true; |
| 121 | + if (Character.isDigit(ch)) hasDigit = true; |
| 122 | + if (specialChars.indexOf(ch) != -1) hasSpecial = true; |
| 123 | + if (i > 0 && ch == password.charAt(i - 1)) return false; |
| 124 | + } |
| 125 | +
|
| 126 | + return hasLower && hasUpper && hasDigit && hasSpecial; |
| 127 | + } |
| 128 | +
|
| 129 | + public static void main(String[] args) { |
| 130 | + String password1 = "IloveLe3tcode!"; |
| 131 | + System.out.println(isStrongPassword(password1)); // Output: true |
| 132 | +
|
| 133 | + String password2 = "Me+You--IsMyDream"; |
| 134 | + System.out.println(isStrongPassword(password2)); // Output: false |
| 135 | +
|
| 136 | + String password3 = "1aB!"; |
| 137 | + System.out.println(isStrongPassword(password3)); // Output: false |
| 138 | + } |
| 139 | +} |
| 140 | +
|
| 141 | +``` |
| 142 | + |
| 143 | +### Python Solution |
| 144 | + |
| 145 | +```python |
| 146 | +def is_strong_password(password): |
| 147 | + if len(password) < 8: |
| 148 | + return False |
| 149 | + |
| 150 | + has_lower = has_upper = has_digit = has_special = False |
| 151 | + special_chars = "!@#$%^&*()-+" |
| 152 | + |
| 153 | + for i in range(len(password)): |
| 154 | + if password[i].islower(): |
| 155 | + has_lower = True |
| 156 | + if password[i].isupper(): |
| 157 | + has_upper = True |
| 158 | + if password[i].isdigit(): |
| 159 | + has_digit = True |
| 160 | + if password[i] in special_chars: |
| 161 | + has_special = True |
| 162 | + if i > 0 and password[i] == password[i - 1]: |
| 163 | + return False |
| 164 | + |
| 165 | + return has_lower and has_upper and has_digit and has_special |
| 166 | + |
| 167 | +# Example usage: |
| 168 | +password1 = "IloveLe3tcode!" |
| 169 | +print(is_strong_password(password1)) # Output: True |
| 170 | + |
| 171 | +password2 = "Me+You--IsMyDream" |
| 172 | +print(is_strong_password(password2)) # Output: False |
| 173 | + |
| 174 | +password3 = "1aB!" |
| 175 | +print(is_strong_password(password3)) # Output: False |
| 176 | + |
| 177 | + |
| 178 | + |
| 179 | +``` |
| 180 | + |
| 181 | +### Complexity Analysis |
| 182 | + |
| 183 | +### Time Complexity: $O(n)$ |
| 184 | + |
| 185 | +> **Reason**:Where n is the length of the password. Each character is checked once. |
| 186 | +
|
| 187 | +### Space Complexity: $O(1)$ |
| 188 | + |
| 189 | +> **Reason**: Only a constant amount of extra space is used. |
| 190 | +
|
| 191 | + |
0 commit comments