Input: password ="Me+You--IsMyDream"Output: falseExplanation: The password does not contain a digit and also contains 2 of the same character in adjacent positions. Therefore, we returnfalse.
classSolution {
publicbooleanstrongPasswordCheckerII(String password) {
if (password.length() < 8) returnfalse;
boolean hasLower =false, hasUpper =false, hasDigit =false, hasSpecial =false;
String special ="!@#$%^&*()-+";
for (int i = 0; i < password.length(); ++i) {
char c = password.charAt(i);
if (i > 0 && c == password.charAt(i-1)) returnfalse;
if (Character.isLowerCase(c)) hasLower =true;
if (Character.isUpperCase(c)) hasUpper =true;
if (Character.isDigit(c)) hasDigit =true;
if (special.indexOf(c) !=-1) hasSpecial =true;
}
return hasLower && hasUpper && hasDigit && hasSpecial;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
funstrongPasswordCheckerII(password: String): Boolean {
if (password.length < 8) returnfalseval special = "!@#$%^&*()-+"var hasLower = false; var hasUpper = false; var hasDigit = false; var hasSpecial = falsefor (i in password.indices) {
val c = password[i]
if (i > 0&& c == password[i-1]) returnfalseif (c.isLowerCase()) hasLower = trueif (c.isUpperCase()) hasUpper = trueif (c.isDigit()) hasDigit = trueif (special.contains(c)) hasSpecial = true }
return hasLower && hasUpper && hasDigit && hasSpecial
}
1
2
3
4
5
6
7
8
9
10
11
12
13
defstrongPasswordCheckerII(password: str) -> bool:
if len(password) <8:
returnFalse has_lower = has_upper = has_digit = has_special =False special = set("!@#$%^&*()-+")
for i, c in enumerate(password):
if i >0and c == password[i-1]:
returnFalseif c.islower(): has_lower =Trueif c.isupper(): has_upper =Trueif c.isdigit(): has_digit =Trueif c in special: has_special =Truereturn has_lower and has_upper and has_digit and has_special
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
pubfnstrong_password_checker_ii(password: String) -> bool {
if password.len() <8 { returnfalse; }
let special ="!@#$%^&*()-+";
letmut has_lower =false;
letmut has_upper =false;
letmut has_digit =false;
letmut has_special =false;
let bytes = password.as_bytes();
for i in0..bytes.len() {
let c = bytes[i] aschar;
if i >0&& bytes[i] == bytes[i-1] { returnfalse; }
if c.is_ascii_lowercase() { has_lower =true; }
if c.is_ascii_uppercase() { has_upper =true; }
if c.is_ascii_digit() { has_digit =true; }
if special.contains(c) { has_special =true; }
}
has_lower && has_upper && has_digit && has_special
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
functionstrongPasswordCheckerII(password: string):boolean {
if (password.length<8) returnfalse;
constspecial="!@#$%^&*()-+";
lethasLower=false, hasUpper=false, hasDigit=false, hasSpecial=false;
for (leti=0; i<password.length; i++) {
constc=password[i];
if (i>0&&c===password[i-1]) returnfalse;
if (c>='a'&&c<='z') hasLower=true;
if (c>='A'&&c<='Z') hasUpper=true;
if (c>='0'&&c<='9') hasDigit=true;
if (special.includes(c)) hasSpecial=true;
}
returnhasLower&&hasUpper&&hasDigit&&hasSpecial;
}