首页 > 其他分享 >2299. 强密码检测器(LeetCode)

2299. 强密码检测器(LeetCode)

时间:2023-01-20 06:44:06浏览次数:43  
标签:字符 2299 ch False hasLower 检测器 password True LeetCode

#19/01/2023. 2299题
class Solution: def strongPasswordCheckerII(self, password: str) -> bool: if len(password) < 8: return False specials = set("!@#$%^&*()-+") hasLower = hasUpper = hasDigit = hasSpecial = False for i, ch in enumerate(password): if i != len(password) - 1 and password[i] == password[i + 1]: return False if ch.islower(): hasLower = True elif ch.isupper(): hasUpper = True elif ch.isdigit(): hasDigit = True elif ch in specials: hasSpecial = True return hasLower and hasUpper and hasDigit and hasSpecial

#题目来自LeetCode

  

 

首先,它会创建一个特殊字符集合"specials",然后定义4个标志位hasLower、hasUpper、hasDigit和hasSpecial,初始值都是False。

然后,它会使用一个for循环遍历password中的每一个字符。在遍历过程中,如果发现当前字符和下一个字符相同,则立即返回False。

然后,对于每个字符,如果它是小写字母,则将hasLower标志位设为True; 如果它是大写字母,则将hasUpper标志位设为True;如果它是数字,则将hasDigit标志位设为True;如果它是specials集合中的字符,则将hasSpecial标志位设为True。

最后,在循环结束后,如果hasLower、hasUpper、hasDigit和hasSpecial标志位都为True,则返回True,表示密码符合要求; 否则返回False,表示密码不符合要求。

 

ch.islower() 是 Python 中字符串类型的一个内置函数。 它用于检查一个字符是否是小写字母。

在这个代码中,ch 遍历到的password中的每一个字符, ch.islower() 会返回一个布尔值,如果 ch 是小写字母,它会返回True, 否则返回False。

这里使用ch.islower()的原因是为了检查password中是否包含小写字母。如果password中包含小写字母,则 hasLower 会被设置为 True。

类似的, ch.isupper() 用于检查字符是否是大写字母, ch.isdigit() 用于检查字符是否是数字,这样就可以检查密码中是否包含大写字母和数字。

标签:字符,2299,ch,False,hasLower,检测器,password,True,LeetCode
From: https://www.cnblogs.com/OrangeCat2002/p/17062382.html

相关文章