Skip to content

Commit 698e246

Browse files
added detect capital use java code
1 parent 204a45c commit 698e246

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

Java/detect-capital-use.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class 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+
}

0 commit comments

Comments
 (0)