-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathStrong_Password.java
More file actions
125 lines (86 loc) · 3.26 KB
/
Strong_Password.java
File metadata and controls
125 lines (86 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
Problem Statement:
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 = "!@#$%^&*()-+"
Input Format
The first line contains an integer denoting the length of the string.
The second line contains a string consisting of characters, the password typed by Louise. Each character is either a lowercase/uppercase English alphabet, a digit, or a special character.
Constraints
Output Format
Print a single line containing a single integer denoting the answer to the problem.
Sample Input 0
3
Ab1
Sample Output 0
3
Explanation 0
She can make the password strong by adding characters, for example, $hk, turning the password into Ab1$hk which is strong.
characters aren't enough since the length must be at least .
Sample Input 1
11
#HackerRank
Sample Output 1
1
Explanation 1
The password isn't strong, but she can make it strong by adding a single digit.
Solution:
*/
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
// Complete the minimumNumber function below.
static int minimumNumber(int n, String password) {
// Return the minimum number of characters to make the password strong
int count=4;
Pattern uppercase = Pattern.compile("[A-Z]");
Pattern lowercase = Pattern.compile("[a-z]");
Pattern digit = Pattern.compile("[0–9]");
Pattern special =Pattern.compile("[!@#$%^&*()-+]");
if(uppercase.matcher(password).find())
{
count -- ;
}
if(lowercase.matcher(password).find())
{
count -- ;
}
if(digit.matcher(password).find())
{
count -- ;
}
if(special.matcher(password).find())
{
count -- ;
}
int k = Math.max(count,6-n);
System.out.println(k);
return k;
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int n = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
String password = scanner.nextLine();
int answer = minimumNumber(n, password);
bufferedWriter.write(String.valueOf(answer));
bufferedWriter.newLine();
bufferedWriter.close();
scanner.close();
}
}