2299. Strong Password Checker II — Daily Problem
Beat 100% of users on execution time. Not much to say about this problem except for one bit manipulation technique.
My Code
class Solution:
def strongPasswordCheckerII(self, password: str) -> bool:
flag1 = flag2 = flag3 = flag4 = 1
ret = None
for i in password:
if ret == i:
return False
if i.isdigit():
flag1 = 0
elif i.isupper():
flag2 = 0
elif i.islower():
flag3 = 0
else:
flag4 = 0
ret = i
if sum([flag1, flag2, flag3, flag4]) == 0 and len(password) >= 8:
return True
return FalseBit Manipulation Code
Method 1: Simulation + Bit Manipulation
Based on the problem description, we simulate the process of checking whether a password meets the requirements.
First, check if the password length is less than 8 — if so, return False.
Then use a bitmask mask to track whether the password contains lowercase letters, uppercase letters, digits, and special characters. Traverse the password: for each character, first check if it's the same as the previous one — if so, return False. Then update mask based on the character type.
Finally, check whether mask == 15 — if so, return True, otherwise return False.
class Solution:
def strongPasswordCheckerII(self, password: str) -> bool:
if len(password) < 8:
return False
mask = 0
for i, c in enumerate(password):
if i and c == password[i - 1]:
return False
if c.islower():
mask |= 1
elif c.isupper():
mask |= 2
elif c.isdigit():
mask |= 4
else:
mask |= 8
return mask == 15Author: ylb
Link: https://leetcode.cn/problems/strong-password-checker-ii/solutions/2068878/by-lcbin-hk2a/
贡献者
最近更新
Involution Hell© 2026 byCommunityunderCC BY-NC-SA 4.0
2293. Min Max Game — Daily Problem
LeetCode 2293. Min Max Game — Recursively reduce an array by pairing elements and alternating min/max comparisons, using a 2D list to avoid index confusion. For intermediate developers.
One question daily 2299. Code inspection device II
LeetCode 2299. 强密码检验器 II 题解 — 模拟与位运算检查密码强度,使用掩码 mask 记录是否包含小写字母、大写字母、数字和特殊字符,并确保无连续相同字符。适合刷题求职者、准备算法面试的 CS 学生。