MCPcopy Create free account
hub / github.com/codedecks-in/LeetCode-Solutions / detectCapitalUse

Method detectCapitalUse

Java/detect-capital-use.java:2–44  ·  view source on GitHub ↗
(String word)

Source from the content-addressed store, hash-verified

1class Solution {
2 public boolean detectCapitalUse(String word) {
3
4 int upperCaseCount = 0;
5 int lowerCaseCount = 0;
6
7 int wordLen = word.length();
8
9 int i = 0;
10 int firstCharFlag = 0;
11
12 for(i=0 ; i<wordLen; i++){
13 int ch = word.charAt(i);
14
15 if(ch >= 'a' && ch <= 'z'){
16 lowerCaseCount++;
17 }
18 else if(ch >= 'A' && ch <= 'Z'){
19 upperCaseCount++;
20
21 if(i == 0){
22 firstCharFlag++;
23 }
24 }
25 }
26
27
28 if(i == wordLen){
29 if(lowerCaseCount == wordLen){
30 return true;
31 }
32 else if(upperCaseCount == wordLen){
33 return true;
34 }
35 else if(firstCharFlag == 1 && upperCaseCount == 1 && lowerCaseCount == (wordLen-1)){
36 return true;
37 }
38 else{
39 return false;
40 }
41 }
42
43 return false;
44 }
45}

Callers

nothing calls this directly

Calls 1

lengthMethod · 0.80

Tested by

no test coverage detected