forked from sowon-dev/AlgorithmStudy_Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrongPassword.java
More file actions
73 lines (57 loc) ยท 2.28 KB
/
StrongPassword.java
File metadata and controls
73 lines (57 loc) ยท 2.28 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
package hackerrank;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StrongPassword {
static int minimumNumber(int n, String password) {
// ์กฐ๊ฑด์ ๋ง๋ 6์๋ฆฌ ์ด์์ ๋น๋ฒ์ด ๋๊ธฐ์ํด์ ํ์ํ ์๋ฆฌ์๋ฅผ ์ถ๋ ฅํ๋ ๋ฌธ์
//sol1
/*
String numbers = "0123456789";
String lower_case = "abcdefghijklmnopqrstuvwxyz";
String upper_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String special_characters = "!@#$%^&*()-+";
//๊ฐ๊ฐ ์กฐ๊ฑด์ booleanํํ๋ก ์ ์
Boolean isNumbers = false;
Boolean isLower_case = false;
Boolean isUpper_case = false;
Boolean isSpecial_characters = false;
int minNum = 0;
//์กฐ๊ฑด์ด ์ฐธ์ด๋ฉด ํด๋น ์กฐ๊ฑด ๊ฐ์ true๋ก ๋ณ๊ฒฝ
for (String s : password.split("")) {
if(numbers.contains(s)) isNumbers = true;
if(lower_case.contains(s)) isLower_case = true;
if(upper_case.contains(s)) isUpper_case = true;
if(special_characters.contains(s)) isSpecial_characters = true;
}
//๊ฐ ์กฐ๊ฑด์ด true๊ฐ ์๋๋ฉด ํ์ํ ์๋ฆฌ์ 1์ฆ๊ฐ
if(!isNumbers) minNum++;
if(!isLower_case) minNum++;
if(!isUpper_case) minNum++;
if(!isSpecial_characters) minNum++;
return minNum < 6 - n ? 6 - n : minNum;
*/
//sol2 ์ ๊ท์์ฌ์ฉ
int minNum = 0;
Pattern numbers = Pattern.compile("[0-9]");
Matcher m = numbers.matcher(password);
if(!m.find()) minNum++;
Pattern lower_case = Pattern.compile("[a-z]");
m = lower_case.matcher(password);
if(!m.find()) minNum++;
Pattern upper_case = Pattern.compile("[A-Z]");
m = upper_case.matcher(password);
if(!m.find()) minNum++;
Pattern special_characters = Pattern.compile("[!@#$%^&*()\\-+]");
m = special_characters.matcher(password);
if(!m.find()) minNum++;
return 6 > minNum + n ? 6 - n : minNum;
}
public static void main(String[] args) {
System.out.println(minimumNumber(3, "Ab1") + ", ans: 3");
System.out.println(minimumNumber(4, "Ab1!") + ", ans: 2");
System.out.println(minimumNumber(6, "Ab1!12") + ", ans: 0");
System.out.println(minimumNumber(11, "#HackearRnk") + ", ans: 1");
System.out.println(minimumNumber(4, "4700") + ", ans: 3");
System.out.println(minimumNumber(4, "IGEC") + ", ans: 3");
}
}