File tree Expand file tree Collapse file tree 2 files changed +75
-0
lines changed
Problems/arrays-and-hashing Expand file tree Collapse file tree 2 files changed +75
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * 520
3
+ * Detect Capital
4
+ **
5
+ * We define the usage of capitals in a word to be right
6
+ * when one of the following cases holds:
7
+ * • All letters in this word are capitals, like "USA".
8
+ * • All letters in this word are not capitals, like "leetcode".
9
+ * • Only the first letter in this word is capital, like "Google".
10
+ *
11
+ * Given a string word,
12
+ * return true if the usage of capitals in it is right.
13
+ *
14
+ * Example 1:
15
+ * Input: word = "USA"
16
+ * Output: true
17
+ *
18
+ * Example 2:
19
+ * Input: word = "FlaG"
20
+ * Output: false
21
+ *
22
+ * Constraints:
23
+ * • 1 <= word.length <= 100
24
+ * • word consists of lowercase and uppercase English letters.
25
+ **
26
+ * https://leetcode.com/problems/detect-capital/
27
+ ***/
28
+
29
+ namespace Problems ;
30
+
31
+ public class DetectCapital
32
+ {
33
+ public bool DetectCapitalUse ( string word )
34
+ {
35
+ bool isAllCapitals = char . IsUpper ( word [ 0 ] ) ;
36
+ bool isAllNotCapitals = char . IsLower ( word [ 0 ] ) ;
37
+ bool isFirstOnlyCapital = char . IsUpper ( word [ 0 ] ) ;
38
+
39
+ for ( int i = 1 ; i < word . Length ; i ++ )
40
+ {
41
+ if ( isAllCapitals && char . IsLower ( word [ i ] ) )
42
+ {
43
+ isAllCapitals = false ;
44
+ }
45
+
46
+ if ( isAllNotCapitals && char . IsUpper ( word [ i ] ) )
47
+ {
48
+ isAllNotCapitals = false ;
49
+ }
50
+
51
+ if ( isFirstOnlyCapital && char . IsUpper ( word [ i ] ) )
52
+ {
53
+ isFirstOnlyCapital = false ;
54
+ }
55
+
56
+ if ( ! isAllCapitals && ! isAllNotCapitals && ! isFirstOnlyCapital )
57
+ {
58
+ return false ;
59
+ }
60
+ }
61
+
62
+ return true ;
63
+ }
64
+ }
Original file line number Diff line number Diff line change
1
+ using NUnit . Framework ;
2
+
3
+ using Problems ;
4
+
5
+ public class DetectCapitalTests
6
+ {
7
+ [ TestCase ( "USA" , ExpectedResult = true ) ]
8
+ [ TestCase ( "FlaG" , ExpectedResult = false ) ]
9
+ public bool DetectCapitalUseTest ( string word ) =>
10
+ new DetectCapital ( ) . DetectCapitalUse ( word ) ;
11
+ }
You can’t perform that action at this time.
0 commit comments