2299. Strong Password Checker II

题目

A password is said to be strong if it satisfies all the following criteria:

It has at least 8 characters.
It contains at least one lowercase letter.
It contains at least one uppercase letter.
It contains at least one digit.
It contains at least one special character. The special characters are the characters in the following string: “!@#$%^&*()-+”.
It does not contain 2 of the same character in adjacent positions (i.e., “aab” violates this condition, but “aba” does not).
Given a string password, return true if it is a strong password. Otherwise, return false.

Example 1:

Input: password = "IloveLe3tcode!"
Output: true
Explanation: The password meets all the requirements. Therefore, we return true.

Example 2:

Input: password = "Me+You--IsMyDream"
Output: false
Explanation: The password does not contain a digit and also contains 2 of the same character in adjacent positions. Therefore, we return false.

Example 3:

Input: password = "1aB!"
Output: false
Explanation: The password does not meet the length requirement. Therefore, we return false.

Constraints:

1 <= password.length <= 100
password consists of letters, digits, and special characters: "!@#$%^&*()-+".

解题思路

简单题,注意大小写可以用py自带的lower()和upper()来判断是否和原字符串相等,另外set化string会直接返回set of character

代码

class Solution:
    def strongPasswordCheckerII(self, password: str) -> bool:
        is_strong_password = True
        # lengh checker
        if len(password) < 8:
            is_strong_password = False
        # lowercase / uppercase
        if password == password.lower() or password == password.upper():
            is_strong_password = False
        # digit
        if not set(map(str, range(0, 10))) & set(password):
            is_strong_password = False
        # special character
        if not set('!@#$%^&*()-+') & set(password):
            is_strong_password = False
        # consective character
        for i in range(len(password) - 1):
            if password[i] == password[i + 1]:
                is_strong_password = False
                break
        return is_strong_password

另外还看到了用掩码来判断各个状态的方法,感觉从代码角度更高效一些

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值