You are viewing a single comment's thread. Return to all comments →
JAVA & JAVASCRIPT Solutions. No Regex. Great example for anyone familiar with one language and learning the other.
Time Complexity: O(n) ... dependent on inputs Space Complexity: O(1)... indepent of of inputs
JAVASCRIPT:
function minimumNumber(n, password) { // Return the minimum number of characters to make the password strong
let missingCharsCount; const numberStr = "0123456789"; const lowercase = "abcdefghijklmnopqrstuvwxyz"; const uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; const specialCharacters = "!@#$%^&*()-+"; const passwordRequirementCount = { length: 6, digits: 1, lowercase: 1, uppercase: 1, specialCharacters: 1 } const condition = (str, char) => { return str.includes(char); } for (const currChar of password) { if (condition(numberStr, currChar)) { if (passwordRequirementCount.digits <= 0) continue; passwordRequirementCount.digits--; } else if (condition(lowercase, currChar)) { if (passwordRequirementCount.lowercase <= 0) continue; passwordRequirementCount.lowercase--; } else if (condition(uppercase, currChar)) { if (passwordRequirementCount.uppercase <= 0) continue; passwordRequirementCount.uppercase--; } else if (condition(specialCharacters, currChar)) { if (passwordRequirementCount.specialCharacters <= 0) continue; passwordRequirementCount.specialCharacters--; } } missingCharsCount = ( passwordRequirementCount.digits + passwordRequirementCount.lowercase + passwordRequirementCount.uppercase + passwordRequirementCount.specialCharacters ); const countTypeReqSatisfied = n + missingCharsCount; if (countTypeReqSatisfied < passwordRequirementCount.length) { const difference = passwordRequirementCount.length - countTypeReqSatisfied; missingCharsCount += difference; return missingCharsCount; } else { return missingCharsCount; }
JAVA:
public class solution { static class PasswordRequirementCount { static int length; static int digits; static int lowercase; static int uppercase; static int specialCharacters; } public static boolean condition(String str, char c) { return str.contains(String.valueOf(c)); } public static int minimumNumber(int n, String password) { // Return the minimum number of characters to make the password strong // Provided variables String numberStr = "0123456789"; String lowercase = "abcdefghijklmnopqrstuvwxyz"; String uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; String specialCharacters = "!@#$%^&*()-+"; new PasswordRequirementCount(); PasswordRequirementCount.length = 6; PasswordRequirementCount.digits = 1; PasswordRequirementCount.lowercase = 1; PasswordRequirementCount.uppercase = 1; PasswordRequirementCount.specialCharacters = 1; for (char ch : password.toCharArray()) { if (condition(numberStr, ch)) { if (PasswordRequirementCount.digits <= 0) continue; PasswordRequirementCount.digits--; } else if (condition(lowercase, ch)) { if (PasswordRequirementCount.lowercase <= 0) continue; PasswordRequirementCount.lowercase--; } else if (condition(uppercase, ch)) { if (PasswordRequirementCount.uppercase <= 0) continue; PasswordRequirementCount.uppercase--; } else if (condition(specialCharacters, ch)) { if (PasswordRequirementCount.specialCharacters <= 0) continue; PasswordRequirementCount.specialCharacters--; } } ; int missingCharsCount = (PasswordRequirementCount.digits + PasswordRequirementCount.lowercase + PasswordRequirementCount.uppercase + PasswordRequirementCount.specialCharacters); int countTypeReqSatisfied = n + missingCharsCount; if (countTypeReqSatisfied < PasswordRequirementCount.length) { int difference = PasswordRequirementCount.length - countTypeReqSatisfied; missingCharsCount += difference; return missingCharsCount; } else { return missingCharsCount; } } } }
Seems like cookies are disabled on this browser, please enable them to open this website
Strong Password
You are viewing a single comment's thread. Return to all comments →
JAVA & JAVASCRIPT Solutions. No Regex. Great example for anyone familiar with one language and learning the other.
Time Complexity: O(n) ... dependent on inputs Space Complexity: O(1)... indepent of of inputs
JAVASCRIPT:
function minimumNumber(n, password) { // Return the minimum number of characters to make the password strong
JAVA: