-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVerifyPassword.java
59 lines (49 loc) · 1.55 KB
/
VerifyPassword.java
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
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class VerifyPassword {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
for (int i = 0; i < t; i++) {
int n = scanner.nextInt();
scanner.nextLine();
String input = scanner.nextLine().trim();
if (!isValid(n, input)) {
System.out.println("NO");
} else {
System.out.println("YES");
}
}
scanner.close();
}
public static boolean isValid(int n, String input) {
List<Character> digits = new ArrayList<>();
List<Character> alphabets = new ArrayList<>();
boolean gotChar = false;
for (char character : input.toCharArray()) {
if (Character.isDigit(character)) {
if (gotChar) {
return false;
} else {
digits.add(character);
}
} else if (Character.isLowerCase(character)) {
gotChar = true;
alphabets.add(character);
}
if (!isSorted(alphabets) || !isSorted(digits)) {
return false;
}
}
return true;
}
public static boolean isSorted(List<Character> list) {
for (int i = 1; i < list.size(); i++) {
if (list.get(i) < list.get(i - 1)) {
return false;
}
}
return true;
}
}