Strong Password

Sort by

recency

|

929 Discussions

|

  • + 0 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;
            }
        }
    }   
    
    }
    
  • + 0 comments
    # Strong Password 🔑
    def minimum_number(n, password):
        res, p = 0, set(password)
        types = [ set("0123456789"), 
                 set("abcdefghijklmnopqrstuvwxyz"), 
                 set("ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 
                 set("!@#$%^&*()-+")]
        res = sum([ 1 for t in types if p - t == p ])
        return max(res, 6 - n)
    
  • + 0 comments

    Javascript code solution

    function minimumNumber(n, password) {
          let result=0;
          /[a-z]{1,}/.test(password.toString())?'':result++;
          /[A-Z]{1,}/.test(password.toString())?'':result++;
          /[0-9]{1,}/.test(password.toString())?'':result++;
          /[!@#$%^&*()+]{1,}|\-/.test(password.toString())?'':result++;
          if((n+result)<6){
            result+=Math.abs((result+password.toString().length)-6);
          }
          return result;
    }
    
  • + 0 comments

    Python Code Solution

    def minimumNumber(n:int, password:str):
        conditions = {"numbers":0,"lower_case":0,"upper_case":0,"special_characters":0}
        for letter in range(n):
            if password[letter].isdigit():
                conditions["numbers"] +=1
            elif password[letter].islower():
                conditions["lower_case"] +=1
            elif password[letter].isupper():
                conditions["upper_case"] +=1
            elif password[letter].isalnum() == False:
                conditions["special_characters"] +=1
        missing_types = len([v for v in conditions.values() if v == 0])
        return max(missing_types, 6 - n)
    
  • + 0 comments

    My Solution in C :

    int isDigit(char ch){
        return (ch>='0' && ch<='9')?1:0;
    }
    
    int isLower(char ch){
        return (ch>='a' && ch<='z')?1:0;
    }
    
    int isUpper(char ch){
        return (ch>='A' && ch<='Z')?1:0;
    }
    
    
    int minimumNumber(int n, char* password) {
        int has_digit = 0;
        int has_lowercase = 0;
        int has_uppercase = 0;
        int has_special = 0;
        char *pass = password;
        char special_chars[] = "!@#$%^&*()-+";
    
        while(*pass){
            if(isDigit(*pass))
                has_digit=1;
            else if(isLower(*pass))
                has_lowercase=1;
            else if(isUpper(*pass))
                has_uppercase=1;
            else{
                for(int i=0; i<strlen(special_chars);i++){
                    if(special_chars[i]==*pass){
                        has_special=1;
                        break;
                    }
                }
            }
            pass++;
        }
    
        int missing_types = 0;
        if (!has_digit)
            missing_types++;
        if (!has_lowercase)
            missing_types++;
        if (!has_uppercase)
            missing_types++;
        if (!has_special)
            missing_types++;
    
        int needed_for_length = 0;
        if (n < 6) {
            needed_for_length = 6 - n;
        }
        int max=0;
        if(needed_for_length>missing_types)
            max = needed_for_length;
        else
            max = missing_types;
        return max;
    }
    

    `