FRom hackerrank

  • Its length is at least .
  • It contains at least one digit.
  • It contains at least one lowercase English character.
  • It contains at least one uppercase English character.
  • It contains at least one special character. The special characters are: !@#$%^&*()-+
She typed a random string of length  in the password field but wasn't sure if it was strong. Given the string she typed, can you find the minimum number of characters she must add to make her password strong?
numbers = "0123456789"
lower_case = "abcdefghijklmnopqrstuvwxyz"
upper_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
special_characters = "!@#$%^&*()-+"
Solution in C# -

class Solution {

    static int minimumNumber(int n, string password) {
        // Return the minimum number of characters to make the password strong
        bool hasUpperCase      = false ;
        bool hasLowerCase     = false ;
        bool hasDecimalDigit         = false ;
        bool hasSpecialChar          = false ;
        //!@#$%^&*()-+
    foreach (char c in password )
    {
      if(char.IsUpper(c))
      hasUpperCase = true;
      else if(char.IsLower(c))
      hasLowerCase = true;
      else if(char.IsDigit(c))
      hasDecimalDigit = true;
      else if(c == '!' || c == '@' || c == '#' || c == '$' || c == '%' ||c == '^' || c == '&' || c == '*' || c == '(' || c == ')'
               || c == '-' || c == '+')
        hasSpecialChar = true;
    }
    int count = 0;
    if(!hasUpperCase)
    count++;
    if(!hasLowerCase)
    count++;
    if(!hasDecimalDigit)
    count++;
    if(!hasSpecialChar)
    count++;
    //a B@4
    if(n+count >= 6)
    return count;
    else
    {
        return 6-n;
    }
    }

    static void Main(string[] args) {
        TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);

        int n = Convert.ToInt32(Console.ReadLine());

        string password = Console.ReadLine();

        int answer = minimumNumber(n, password);

        textWriter.WriteLine(answer);

        textWriter.Flush();
        textWriter.Close();
    }
}

Post a Comment

Previous Post Next Post