520. Detect Capital

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public boolean detectCapitalUse(String word) {
int upperCaseCount = 0;
for (int i = 0; i < word.length(); i++) {
if (Character.isUpperCase(word.charAt(i))) {
upperCaseCount++;
}
}

return upperCaseCount == word.length() || upperCaseCount == 0 || (Character.isUpperCase(word.charAt(0)) && upperCaseCount == 1);
}
}

References

520. Detect Capital