forked from Sunchit/Coding-Decoded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution_DetectCapital.java
37 lines (33 loc) · 1.09 KB
/
Solution_DetectCapital.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
class Solution {
public boolean detectCapitalUse(String word) {
// USA
// Google
// leetcode
int length = word.length();
if(length == 0|| length==1){
return true;
}
char zeroChar = word.charAt(0);
boolean zeroIsUpper = Character.isUpperCase(zeroChar);
if(zeroIsUpper){
char firstChar = word.charAt(1);
boolean firstIsUpper = Character.isUpperCase(firstChar);
for(int i=2;i<word.length();i++){
char currentChar = word.charAt(i);
boolean currentIsUpper = Character.isUpperCase(currentChar);
if(currentIsUpper != firstIsUpper){
return false;
}
}
} else {
for(int i=1;i<word.length();i++){
char currentChar = word.charAt(i);
boolean currentIsUpper = Character.isUpperCase(currentChar);
if(currentIsUpper != zeroIsUpper){
return false;
}
}
}
return true;
}
}