Skip to content

Commit 9891377

Browse files
committed
New Problem Solution "Detect Capital"
1 parent 30a7cc7 commit 9891377

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ LeetCode
5656
|543|[Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/) | [Python](./algorithms/python/DiameterOfBinaryTree/diameterOfBinaryTree.py)|Easy|
5757
|538|[Convert BST to Greater Tree](https://leetcode.com/problems/convert-bst-to-greater-tree/) | [Python](./algorithms/python/ConvertBSTtoGreaterTree/convertBST.py)|Easy|
5858
|532|[K-diff Pairs in an Array](https://leetcode.com/problems/k-diff-pairs-in-an-array/) | [Python](./algorithms/python/K-diffPairsInAnArray/findPairs.py)|Easy|
59+
|520|[Detect Capital](https://leetcode.com/problems/detect-capital/) | [C++](./algorithms/cpp/detectCapital/DetectCapital.cpp)|Easy|
5960
|509|[Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [Python](./algorithms/python/FibonacciNumber/fib.py)|Easy|
6061
|477|[Total Hamming Distance](https://leetcode.com/problems/total-hamming-distance/) | [C++](./algorithms/cpp/totalHammingDistance/totalHammingDistance.cpp)|Medium|
6162
|450|[DeleteNodeInABST](https://leetcode.com/problems/delete-node-in-a-bst/) | [Python](./algorithms/python/DeleteNodeInABST/deleteNode.py)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Source : https://leetcode.com/problems/detect-capital/
2+
// Author : Hao Chen
3+
// Date : 2019-02-04
4+
5+
/*****************************************************************************************************
6+
*
7+
*
8+
* Given a word, you need to judge whether the usage of capitals in it is right or not.
9+
*
10+
* We define the usage of capitals in a word to be right when one of the following cases holds:
11+
*
12+
* All letters in this word are capitals, like "USA".
13+
* All letters in this word are not capitals, like "leetcode".
14+
* Only the first letter in this word is capital if it has more than one letter, like "Google".
15+
*
16+
* Otherwise, we define that this word doesn't use capitals in a right way.
17+
*
18+
* Example 1:
19+
*
20+
* Input: "USA"
21+
* Output: True
22+
*
23+
* Example 2:
24+
*
25+
* Input: "FlaG"
26+
* Output: False
27+
*
28+
* Note:
29+
* The input will be a non-empty word consisting of uppercase and lowercase latin letters.
30+
******************************************************************************************************/
31+
class Solution {
32+
bool is_lower(char ch) {
33+
return ch >='a' && ch <='z';
34+
}
35+
bool is_upper(char ch) {
36+
return ch >='A' && ch <='Z';
37+
}
38+
bool is_alpha(char ch) {
39+
return is_lower(ch) || is_upper(ch);
40+
}
41+
public:
42+
bool detectCapitalUse(string word) {
43+
bool all_upper = true, all_lower = true, first = is_upper(word[0]);
44+
for(int i=1; i<word.size(); i++) {
45+
if (is_lower(word[i])) all_upper = false;
46+
if (is_upper(word[i])) all_lower = false;
47+
}
48+
return all_lower || first && all_upper;
49+
}
50+
};

0 commit comments

Comments
 (0)