|
1 | 1 | packagecom.fishercoder.solutions;
|
2 | 2 |
|
3 | 3 | /**
|
| 4 | + * 520. Detect Capital |
| 5 | + * |
4 | 6 | * Given a word, you need to judge whether the usage of capitals in it is right or not.
|
5 |
| -
|
6 |
| - We define the usage of capitals in a word to be right when one of the following cases holds: |
| 7 | + * We define the usage of capitals in a word to be right when one of the following cases holds: |
7 | 8 |
|
8 | 9 | All letters in this word are capitals, like "USA".
|
9 | 10 | All letters in this word are not capitals, like "leetcode".
|
10 | 11 | Only the first letter in this word is capital if it has more than one letter, like "Google".
|
11 | 12 | Otherwise, we define that this word doesn't use capitals in a right way.
|
| 13 | +
|
12 | 14 | Example 1:
|
13 | 15 | Input: "USA"
|
14 | 16 | Output: True
|
| 17 | +
|
15 | 18 | Example 2:
|
16 | 19 | Input: "FlaG"
|
17 | 20 | Output: False
|
18 |
| - Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters. |
19 | 21 |
|
| 22 | + Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters. |
20 | 23 | */
|
21 | 24 | publicclass_520 {
|
22 |
| -publicbooleandetectCapitalUse(Stringword) { |
23 |
| -char[]words =word.toCharArray(); |
24 |
| -booleanfirstLetterCap =false; |
25 |
| -if (Character.isUpperCase(words[0])) { |
26 |
| -firstLetterCap =true; |
27 |
| - } |
| 25 | +publicstaticclassSolution1 { |
| 26 | +publicbooleandetectCapitalUse(Stringword) { |
| 27 | +char[]words =word.toCharArray(); |
| 28 | +booleanfirstLetterCap =false; |
| 29 | +if (Character.isUpperCase(words[0])) { |
| 30 | +firstLetterCap =true; |
| 31 | + } |
28 | 32 |
|
29 |
| -if (firstLetterCap) { |
30 |
| -if (words.length >=2) { |
31 |
| -inti =2; |
32 |
| -if (Character.isUpperCase(words[1])) { |
33 |
| -//then all following must be all uppercase |
34 |
| -while (i <words.length) { |
35 |
| -if (!Character.isUpperCase(words[i])) { |
36 |
| -returnfalse; |
| 33 | +if (firstLetterCap) { |
| 34 | +if (words.length >=2) { |
| 35 | +inti =2; |
| 36 | +if (Character.isUpperCase(words[1])) { |
| 37 | +//then all following must be all uppercase |
| 38 | +while (i <words.length) { |
| 39 | +if (!Character.isUpperCase(words[i])) { |
| 40 | +returnfalse; |
| 41 | + } |
| 42 | +i++; |
37 | 43 | }
|
38 |
| -i++; |
39 |
| - } |
40 |
| -returntrue; |
41 |
| -}else { |
42 |
| -//then all following must be all lowercase |
43 |
| -while (i <words.length) { |
44 |
| -if (!Character.isLowerCase(words[i])) { |
45 |
| -returnfalse; |
| 44 | +returntrue; |
| 45 | + }else { |
| 46 | +//then all following must be all lowercase |
| 47 | +while (i <words.length) { |
| 48 | +if (!Character.isLowerCase(words[i])) { |
| 49 | +returnfalse; |
| 50 | + } |
| 51 | +i++; |
46 | 52 | }
|
47 |
| -i++; |
| 53 | +returntrue; |
48 | 54 | }
|
49 |
| -returntrue; |
50 | 55 | }
|
51 |
| - } |
52 |
| -returntrue; |
53 |
| - }else { |
54 |
| -//then all following must be all lowercase |
55 |
| -inti =1; |
56 |
| -while (i <words.length) { |
57 |
| -if (!Character.isLowerCase(words[i])) { |
58 |
| -returnfalse; |
| 56 | +returntrue; |
| 57 | + }else { |
| 58 | +//then all following must be all lowercase |
| 59 | +inti =1; |
| 60 | +while (i <words.length) { |
| 61 | +if (!Character.isLowerCase(words[i])) { |
| 62 | +returnfalse; |
| 63 | + } |
| 64 | +i++; |
59 | 65 | }
|
60 |
| -i++; |
| 66 | +returntrue; |
61 | 67 | }
|
62 |
| -returntrue; |
63 | 68 | }
|
64 | 69 | }
|
65 | 70 | }
|