Skip to content

Commit e6cf6d2

Browse files
committed
detects captial or not
1 parent 350858c commit e6cf6d2

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package Leetcode_August_Challenge;
2+
3+
/*
4+
QUESTION DETAILS:
5+
=================
6+
Given a word, you need to judge whether the usage of capitals in it is right or not.
7+
8+
We define the usage of capitals in a word to be right when one of the following cases holds:
9+
10+
All letters in this word are capitals, like "USA".
11+
All letters in this word are not capitals, like "leetcode".
12+
Only the first letter in this word is capital, like "Google".
13+
Otherwise, we define that this word doesn't use capitals in a right way.
14+
15+
Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters.
16+
17+
*/
18+
19+
public class Day1_detectCapital {
20+
21+
public static void main(String[] args) {
22+
23+
System.out.println(detectCapitalUse("USA")); // true
24+
System.out.println(detectCapitalUse("leetcode"));// true
25+
System.out.println(detectCapitalUse("Google")); // true
26+
System.out.println(detectCapitalUse("FlaG"));// false
27+
28+
}
29+
30+
public static boolean detectCapitalUse(String word) {
31+
if (word.equals(word.toUpperCase())) {
32+
return true;
33+
} else if (word.equals(word.toLowerCase())) {
34+
return true;
35+
} else if (Character.isUpperCase(word.charAt(0))
36+
& word.substring(1).equals(word.substring(1).toLowerCase())) {
37+
return true;
38+
} else {
39+
return false;
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)