列表

详情


2299. 强密码检验器 II

如果一个密码满足以下所有条件,我们称它是一个  密码:

给你一个字符串 password ,如果它是一个  密码,返回 true,否则返回 false 。

 

示例 1:

输入:password = "IloveLe3tcode!"
输出:true
解释:密码满足所有的要求,所以我们返回 true 。

示例 2:

输入:password = "Me+You--IsMyDream"
输出:false
解释:密码不包含数字,且包含 2 个连续相同的字符。所以我们返回 false 。

示例 3:

输入:password = "1aB!"
输出:false
解释:密码不符合长度要求。所以我们返回 false 。

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
class Solution { public: bool strongPasswordCheckerII(string password) { } };

javascript 解法, 执行用时: 56 ms, 内存消耗: 41 MB, 提交时间: 2023-01-19 08:23:44

/**
 * @param {string} password
 * @return {boolean}
 */
var strongPasswordCheckerII = function(password) {
    return /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()\-+])(?!.*(.)\1+).{8,}$/.test(password)
};

java 解法, 执行用时: 1 ms, 内存消耗: 39.8 MB, 提交时间: 2023-01-19 08:23:20

class Solution {
    public boolean strongPasswordCheckerII(String password) {
        if (password.length() < 8) {
            return false;
        }

        Set<Character> specials = new HashSet<Character>() {{
            add('!');
            add('@');
            add('#');
            add('$');
            add('%');
            add('^');
            add('&');
            add('*');
            add('(');
            add(')');
            add('-');
            add('+');
        }};
        int n = password.length();
        boolean hasLower = false, hasUpper = false, hasDigit = false, hasSpecial = false;
        for (int i = 0; i < n; ++i) {
            if (i != n - 1 && password.charAt(i) == password.charAt(i + 1)) {
                return false;
            }

            char ch = password.charAt(i);
            if (Character.isLowerCase(ch)) {
                hasLower = true;
            } else if (Character.isUpperCase(ch)) {
                hasUpper = true;
            } else if (Character.isDigit(ch)) {
                hasDigit = true;
            } else if (specials.contains(ch)) {
                hasSpecial = true;
            }
        }

        return hasLower && hasUpper && hasDigit && hasSpecial;
    }
}

javascript 解法, 执行用时: 56 ms, 内存消耗: 41.3 MB, 提交时间: 2023-01-19 08:22:54

/**
 * @param {string} password
 * @return {boolean}
 */
var strongPasswordCheckerII = function(password) {
    if (password.length < 8) {
        return false;
    }

    const specials = new Set();
    specials.add('!');
    specials.add('@');
    specials.add('#');
    specials.add('$');
    specials.add('%');
    specials.add('^');
    specials.add('&');
    specials.add('*');
    specials.add('(');
    specials.add(')');
    specials.add('-');
    specials.add('+');
    const n = password.length;
    let hasLower = false, hasUpper = false, hasDigit = false, hasSpecial = false;
    for (let i = 0; i < n; ++i) {
        if (i !== n - 1 && password[i] === password[i + 1]) {
            return false;
        }

        const ch = password[i];
        if (isLowerCase(ch)) {
            hasLower = true;
        } else if (isUpperCase(ch)) {
            hasUpper = true;
        } else if (isDigit(ch)) {
            hasDigit = true;
        } else if (specials.has(ch)) {
            hasSpecial = true;
        }
    }
    return hasLower && hasUpper && hasDigit && hasSpecial;
};

const isDigit = (ch) => {
    return parseFloat(ch).toString() === "NaN" ? false : true;
}

const isLowerCase = str => 'a' <= str && str <= 'z';

const isUpperCase = str => 'A' <= str && str <= 'Z';

golang 解法, 执行用时: 0 ms, 内存消耗: 1.8 MB, 提交时间: 2023-01-19 08:22:40

func strongPasswordCheckerII(password string) bool {
    n := len(password)
    if n < 8 {
        return false
    }

    var hasLower, hasUpper, hasDigit, hasSpecial bool
    for i, ch := range password {
        if i != n-1 && password[i] == password[i+1] {
            return false
        }
        if unicode.IsLower(ch) {
            hasLower = true
        } else if unicode.IsUpper(ch) {
            hasUpper = true
        } else if unicode.IsDigit(ch) {
            hasDigit = true
        } else if strings.ContainsRune("!@#$%^&*()-+", ch) {
            hasSpecial = true
        }
    }

    return hasLower && hasUpper && hasDigit && hasSpecial
}

python3 解法, 执行用时: 40 ms, 内存消耗: 15 MB, 提交时间: 2022-06-14 15:48:33

class Solution:
    def strongPasswordCheckerII(self, password: str) -> bool:
        if len(password) < 8: return False
        has_upper = has_lower = has_digit = has_special = False
        has_double = False
        for i, c in enumerate(password):
            if ord('0') <= ord(c) <= ord('9'):
                has_digit = True
            elif ord('A') <= ord(c) <= ord('Z'):
                has_upper = True
            elif ord('a') <= ord(c) <= ord('z'):
                has_lower = True
            elif c in '~!@#$%^&*()_+-=':
                has_special = True

            if i > 0 and c == password[i-1]:
                has_double = True
        return has_upper and has_lower and has_digit and has_special and not has_double

上一题