Louise joined a social networking site to stay in touch with her friends. The signup page required her to input a name and a password. However, the password must be strong. The website considers a password to be strong if it satisfies the following criteria:
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?
Note: Here's the set of types of characters in a form you can paste in your solution:
numbers = "0123456789"
lower_case = "abcdefghijklmnopqrstuvwxyz"
upper_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
special_characters = "!@#$%^&*()-+"
Example
This password is 5 characters long and is missing an uppercase and a special character. The minimum number of characters to add is .
This password is 5 characters long and has at least one of each character type. The minimum number of characters to add is .
Function Description
Complete the minimumNumber function in the editor below.
minimumNumber has the following parameters:
int n: the length of the password
string password: the password to test
Returns
int: the minimum number of characters to add
Input Format
The first line contains an integer , the length of the password.
The second line contains the password string. Each character is either a lowercase/uppercase English alphabet, a digit, or a special character.
Solution :
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'minimumNumber' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER n
* 2. STRING password
*/
public static int minimumNumber(int n, String password) {
// Return the minimum number of characters to make the password strong
// Define character sets
String numbers = "0123456789";
String lower_case = "abcdefghijklmnopqrstuvwxyz";
String upper_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String special_characters = "!@#$%^&*()-+";
boolean hasdigit=false;
boolean haslowercase=false;
boolean hasuppercase=false;
boolean hasspecial=false;
for(char c:password.toCharArray())
{
if(numbers.indexOf(c)!=-1) hasdigit=true;
else if(lower_case.indexOf(c) !=-1) haslowercase=true;
else if(upper_case.indexOf(c) !=-1) hasuppercase=true;
else if(special_characters.indexOf(c) !=-1) hasspecial=true;
}
// Count missing character types
int missingTypes = 0;
if(!hasdigit) missingTypes++;
if(!haslowercase) missingTypes++;
if(!hasuppercase) missingTypes++;
if(!hasspecial) missingTypes++;
// Ensure total length is at least 6
return Math.max(missingTypes, 6 - n);
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int n = Integer.parseInt(bufferedReader.readLine().trim());
String password = bufferedReader.readLine();
int answer = Result.minimumNumber(n, password);
bufferedWriter.write(String.valueOf(answer));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}
Comments
Post a Comment