2299.强密码检验器II
链接:2299.强密码检验器II
难度:Easy
标签:字符串
简介:给你一个字符串 password ,如果它是一个 强 密码,返回 true,否则返回 false 。
题解 1 - python
- 编辑时间:2023-01-19
- 执行用时:40ms
- 内存消耗:14.9MB
- 编程语言:python
- 解法介绍:同上。
class Solution:
def strongPasswordCheckerII(self, password: str) -> bool:
    spec = "!@#$%^&*()-+"
    f = [False for _ in range(4)]
    for i, c in enumerate(password):
        f[0] |= c.islower()
        f[1] |= c.isupper()
        f[2] |= c.isdigit()
        f[3] |= spec.find(c) != -1
        if i != 0 and password[i - 1] == c:
            return False
    return len(password) >= 8 and f[0] and f[1] and f[2] and f[3]
题解 2 - cpp
- 编辑时间:2023-01-19
- 内存消耗:5.9MB
- 编程语言:cpp
- 解法介绍:遍历。
class Solution {
public:
    bool strongPasswordCheckerII(string password) {
        bool f[4] = {false};
        string spec = "!@#$%^&*()-+";
        for (int i = 0; i < password.size(); i++) {
            if (islower(password[i])) f[0] = true;
            if (isupper(password[i])) f[1] = true;
            if (isdigit(password[i])) f[2] = true;
            if (spec.find(password[i]) != string::npos) f[3] = true;
            if (i != 0 && password[i] == password[i - 1]) return false;
        }
        return f[0] && f[1] && f[2] && f[3] && password.size() >= 8;
    }
};
题解 3 - rust
- 编辑时间:2023-01-19
- 内存消耗:2MB
- 编程语言:rust
- 解法介绍:同上。
impl Solution {
pub fn strong_password_checker_ii(password: String) -> bool {
    let spec = "!@#$%^&*()-+";
    let s = password.chars().collect::<Vec<char>>();
    let mut f = [false; 4];
    for i in 0..s.len() {
        if s[i].is_lowercase() {
            f[0] = true;
        }
        if s[i].is_uppercase() {
            f[1] = true;
        }
        if s[i].is_digit(10) {
            f[2] = true;
        }
        if spec.contains(s[i]) {
            f[3] = true;
        }
        if i > 0 && s[i] == s[i - 1] {
            return false;
        }
    }
    s.len() >= 8 && f[0] && f[1] && f[2] && f[3]
}