Advertisement
Guest User

Strong Password Hackerrank

a guest
Jul 28th, 2020
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.55 KB | None | 0 0
  1.  static int minimumNumber(int n, String password) {
  2.         // Return the minimum number of characters to make the password strong
  3.         char[] pw = password.toCharArray();
  4.         int specialCharacters =0;
  5.         int numbers =0;
  6.         int lowerCase =0;
  7.         int upperCase =0;
  8.         String special_characters = "!@#$%^&*()-+";
  9.         for(int i=0;i<n;i++){
  10.             if((int)pw[i]>=48 && (int)pw[i]<=57)
  11.                 numbers++;
  12.             else if((int)pw[i]>=65 && (int)pw[i]<=90)
  13.                 upperCase++;
  14.             else if((int)pw[i]>=97 && (int)pw[i]<=122)
  15.                 lowerCase++;
  16.             else if(special_characters.contains(""+pw[i]))
  17.                 specialCharacters++;
  18.             if(numbers>=1 && upperCase >=1 && lowerCase>=1 && pw.length>=6 && specialCharacters>=1){
  19.                 return 0;
  20.             }
  21.         }
  22.         if(n >=6){
  23.             int count =0;
  24.             if(numbers==0)
  25.                 count++;
  26.             if(upperCase==0)
  27.                 count++;
  28.             if(lowerCase==0)
  29.                 count++;
  30.             if(specialCharacters==0)
  31.                 count++;
  32.             return count;
  33.         }else{
  34.             int count =0;
  35.             if(numbers==0)
  36.                 count++;
  37.             if(upperCase==0)
  38.                 count++;
  39.             if(lowerCase==0)
  40.                 count++;
  41.             if(specialCharacters==0)
  42.                 count++;
  43.             int ans = count+n;
  44.             if(ans>=6)
  45.                 return count;
  46.             return 6-pw.length;
  47.         }
  48.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement