Strong Password

  • + 0 comments
    public static int minimumNumber(int n, string password)
    {
    // Return the minimum number of characters to make the password strong
        List<char> distinctChars = password.Distinct().ToList<char>();
    
        int charsToBeAdded = 0;
    
        if(!distinctChars.Exists(x => Char.IsAsciiDigit(x) == true))
        {
            charsToBeAdded++;
        }
    
        if(!distinctChars.Exists(x => Char.IsAsciiLetterLower(x) == true))
        {
            charsToBeAdded++;
        }
    
        if(!distinctChars.Exists(x => Char.IsAsciiLetterUpper(x) == true))
        {
            charsToBeAdded++;
        }
    
        if(!distinctChars.Exists(x => Char.IsAsciiLetterOrDigit(x) == false && Char.IsWhiteSpace(x) == false))
        {
            charsToBeAdded++;
        }
    
        const int minPasswordLength = 6;
    
        if(password.Length > minPasswordLength)
        {
            return charsToBeAdded;
        }
        else
        {
            int difference = minPasswordLength - password.Length;
    
            if(difference > charsToBeAdded)
            {
                return difference;
            }
            else
            {
                return charsToBeAdded;
            }
        }
    
    }