-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDetectCapital.java
More file actions
48 lines (39 loc) · 1.15 KB
/
Copy pathDetectCapital.java
File metadata and controls
48 lines (39 loc) · 1.15 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
package LeetcodePractice;
public class DetectCapital {
public static void main(String[] args) {
System.out.println(detectCapitalUse("USA"));
System.out.println(detectCapitalUse("FlaG"));
System.out.println(detectCapitalUse("Google"));
System.out.println(detectCapitalUse("leetcode"));
}
public static boolean detectCapitalUse(String word) {
int n = word.length();
if (n == 0) return true;
if (isAllUpper(word)) {
return true;
}
if (isAllLower(word)) {
return true;
}
if (Character.isUpperCase(word.charAt(0)) && isAllLower(word.substring(1))) {
return true;
}
return false;
}
private static boolean isAllUpper(String s) {
for (char c : s.toCharArray()) {
if (!Character.isUpperCase(c)) {
return false;
}
}
return true;
}
private static boolean isAllLower(String s) {
for (char c : s.toCharArray()) {
if (!Character.isLowerCase(c)) {
return false;
}
}
return true;
}
}