-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathLongestPassword.java
30 lines (28 loc) · 1009 Bytes
/
LongestPassword.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
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S) {
// write your code in Java SE 8
int res = -1;
String[] words = S.split(" ");
for (String word: words) {
int letterCount = 0, digitCount = 0;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if (0x30 <= c && c <= 0x39) {
digitCount++;
} else if ((0x41 <= c && c <= 0x5A) || (0x61 <= c && c <= 0x7A)) {
letterCount++;
} else {
break;
}
}
if (letterCount + digitCount == word.length() && letterCount % 2 == 0 && digitCount % 2 == 1) {
if (res < word.length()) res = word.length();
}
}
return res;
}
}