Easy
题目描述
如果一个密码满足以下所有条件,我们称它是强密码:
- 它有至少 8 个字符。
- 它至少包含一个小写字母。
- 它至少包含一个大写字母。
- 它至少包含一个数字。
- 它至少包含一个特殊字符。特殊字符为:"!@#$%^&*()-+" 中的字符。
- 它 不 包含 2 个连续相同的字符(比如 “aab” 不符合该条件,但是 “aba” 符合该条件)。
给你一个字符串 password ,如果它是一个强密码,返回 true,否则返回 false 。
示例 1:
输入:password = "IloveLe3tcode!"
输出:true
解释:密码满足所有的要求,因此我们返回 true 。
示例 2:
输入:password = "Me+You--IsMyDream"
输出:false
解释:密码不包含数字,且包含 2 个连续相同的字符。因此我们返回 false 。
示例 3:
输入:password = "1aB!"
输出:false
解释:密码不满足长度要求。因此我们返回 false 。
提示:
1 <= password.length <= 100password由字母、数字和特殊字符"!@#$%^&*()-+"组成。
解题思路
这道题要求我们检查密码是否满足所有强密码条件。我们需要逐一验证每个条件:
解题思路:
- 长度检查:首先检查密码长度是否至少为8个字符
- 字符类型检查:使用布尔标记来跟踪是否包含:
- 小写字母 (a-z)
- 大写字母 (A-Z)
- 数字 (0-9)
- 特殊字符 ("!@#$%^&*()-+")
- 相邻字符检查:遍历字符串,检查是否存在连续相同的字符
算法步骤:
- 如果长度小于8,直接返回false
- 初始化四个布尔变量记录各类字符是否出现
- 遍历密码字符串:
- 检查当前字符类型并更新对应标记
- 检查当前字符是否与前一个字符相同
- 最后验证所有条件是否都满足
时间复杂度为O(n),空间复杂度为O(1),其中n是密码长度。这种方法只需要一次遍历就能完成所有检查,是最优解法。
代码实现
class Solution {
public:
bool strongPasswordCheckerII(string password) {
if (password.length() < 8) {
return false;
}
bool hasLower = false, hasUpper = false, hasDigit = false, hasSpecial = false;
string special = "!@#$%^&*()-+";
for (int i = 0; i < password.length(); i++) {
char c = password[i];
// Check for adjacent same characters
if (i > 0 && password[i] == password[i-1]) {
return false;
}
// Check character types
if (c >= 'a' && c <= 'z') {
hasLower = true;
} else if (c >= 'A' && c <= 'Z') {
hasUpper = true;
} else if (c >= '0' && c <= '9') {
hasDigit = true;
} else if (special.find(c) != string::npos) {
hasSpecial = true;
}
}
return hasLower && hasUpper && hasDigit && hasSpecial;
}
};
class Solution:
def strongPasswordCheckerII(self, password: str) -> bool:
if len(password) < 8:
return False
has_lower = has_upper = has_digit = has_special = False
special_chars = "!@#$%^&*()-+"
for i, c in enumerate(password):
# Check for adjacent same characters
if i > 0 and password[i] == password[i-1]:
return False
# Check character types
if c.islower():
has_lower = True
elif c.isupper():
has_upper = True
elif c.isdigit():
has_digit = True
elif c in special_chars:
has_special = True
return has_lower and has_upper and has_digit and has_special
public class Solution {
public bool StrongPasswordCheckerII(string password) {
if (password.Length < 8) {
return false;
}
bool hasLower = false, hasUpper = false, hasDigit = false, hasSpecial = false;
string specialChars = "!@#$%^&*()-+";
for (int i = 0; i < password.Length; i++) {
char c = password[i];
// Check for adjacent same characters
if (i > 0 && password[i] == password[i-1]) {
return false;
}
// Check character types
if (char.IsLower(c)) {
hasLower = true;
} else if (char.IsUpper(c)) {
hasUpper = true;
} else if (char.IsDigit(c)) {
hasDigit = true;
} else if (specialChars.Contains(c)) {
hasSpecial = true;
}
}
return hasLower && hasUpper && hasDigit && hasSpecial;
}
}
var strongPasswordCheckerII = function(password) {
if (password.length < 8) {
return false;
}
let hasLower = false, hasUpper = false, hasDigit = false, hasSpecial = false;
const specialChars = "!@#$%^&*()-+";
for (let i = 0; i < password.length; i++) {
const c = password[i];
// Check for adjacent same characters
if (i > 0 && password[i]
复杂度分析
| 复杂度类型 | 复杂度 | 说明 |
|---|---|---|
| 时间复杂度 | O(n) | 需要遍历一次密码字符串,n为密码长度 |
| 空间复杂度 | O(1) | 只使用了常量级别的额外空间存储布尔标记 |
相关题目
. Strong Password Checker (Hard)
. Validate IP Address (Medium)