6
6
7
7
<p >Given a word, you need to judge whether the usage of capitals in it is right or not.</p >
8
8
9
-
10
-
11
9
<p >We define the usage of capitals in a word to be right when one of the following cases holds:</p >
12
10
13
-
14
-
15
11
<ol >
16
12
<li>All letters in this word are capitals, like "USA".</li>
17
13
<li>All letters in this word are not capitals, like "leetcode".</li>
20
16
21
17
Otherwise, we define that this word doesn' ; t use capitals in a right way.
22
18
23
-
24
-
25
19
<p >  ; </p >
26
20
27
-
28
-
29
21
<p ><b >Example 1:</b ></p >
30
22
31
-
32
-
33
23
<pre >
34
24
35
25
<b >Input:</b > " ; USA" ;
@@ -38,16 +28,10 @@ Otherwise, we define that this word doesn't use capitals in a right way.
38
28
39
29
</pre >
40
30
41
-
42
-
43
31
<p >  ; </p >
44
32
45
-
46
-
47
33
<p ><b >Example 2:</b ></p >
48
34
49
-
50
-
51
35
<pre >
52
36
53
37
<b >Input:</b > " ; FlaG" ;
@@ -56,30 +40,68 @@ Otherwise, we define that this word doesn't use capitals in a right way.
56
40
57
41
</pre >
58
42
59
-
60
-
61
43
<p >  ; </p >
62
44
63
-
64
-
65
45
<p ><b >Note:</b > The input will be a non-empty word consisting of uppercase and lowercase latin letters.</p >
66
46
67
-
68
-
69
47
## Solutions
70
48
71
49
<!-- tabs:start -->
72
50
73
51
### ** Python3**
74
52
75
53
``` python
76
-
54
+ class Solution :
55
+ def detectCapitalUse (self , word : str ) -> bool :
56
+ cnt = 0
57
+ for c in word:
58
+ if c.isupper():
59
+ cnt += 1
60
+ return cnt == 0 or cnt == len (word) or (cnt == 1 and word[0 ].isupper())
77
61
```
78
62
79
63
### ** Java**
80
64
81
65
``` java
66
+ class Solution {
67
+ public boolean detectCapitalUse (String word ) {
68
+ int cnt = 0 ;
69
+ for (char c : word. toCharArray()) {
70
+ if (Character . isUpperCase(c)) {
71
+ ++ cnt;
72
+ }
73
+ }
74
+ return cnt == 0 || cnt == word. length() || (cnt == 1 && Character . isUpperCase(word. charAt(0 )));
75
+ }
76
+ }
77
+ ```
78
+
79
+ ### ** C++**
80
+
81
+ ``` cpp
82
+ class Solution {
83
+ public:
84
+ bool detectCapitalUse(string word) {
85
+ int cnt = 0;
86
+ for (char c : word)
87
+ if (isupper(c)) ++cnt;
88
+ return cnt == 0 || cnt == word.size() || (cnt == 1 && isupper(word[ 0] ));
89
+ }
90
+ };
91
+ ```
82
92
93
+ ### **Go**
94
+
95
+ ```go
96
+ func detectCapitalUse(word string) bool {
97
+ cnt := 0
98
+ for _, c := range word {
99
+ if unicode.IsUpper(c) {
100
+ cnt++
101
+ }
102
+ }
103
+ return cnt == 0 || cnt == len(word) || (cnt == 1 && unicode.IsUpper(rune(word[0])))
104
+ }
83
105
```
84
106
85
107
### ** ...**
0 commit comments