[LeetCode] Strong Password Checker II

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
bool isspecial(char ch) {
string sp = "!@#$%^&*()-+";
return sp.find(ch) != string::npos;
}
public:
bool strongPasswordCheckerII(string s) {
bool lower = false, upper = false, digit = false, special = false;
if(s.length() < 8) return false;
for(int i = 0; i < s.length(); i++) {
if(i + 1 != s.length() and s[i] == s[i+1]) return false;
if(islower(s[i])) lower = true;
else if(isupper(s[i])) upper = true;
else if(isdigit(s[i])) digit = true;
else if(isspecial(s[i])) special = true;
}
return lower and upper and digit and special;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/06/12/PS/LeetCode/strong-password-checker-ii/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.