Skip to content

Commit 4f58316

Browse files
solves detect capital
1 parent 10aee2f commit 4f58316

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137
| 506 | [Relative Ranks](https://leetcode.com/problems/relative-ranks) | Easy | [![Java](assets/java.png)](src/RelativeRanks.java) [![Python](assets/python.png)](python/relative_ranks.py) |
138138
| 507 | [Perfect Number](https://leetcode.com/problems/perfect-number) | Easy | [![Java](assets/java.png)](src/CheckPerfectNumber.java) [![Python](assets/python.png)](python/check_perfect_number.py) |
139139
| 509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number) | Easy | [![Java](assets/java.png)](src/FibonacciNumber.java) [![Python](assets/python.png)](python/fibonacci_number.py) |
140-
| 520 | [Detect Capital](https://leetcode.com/problems/detect-capital) | Easy | |
140+
| 520 | [Detect Capital](https://leetcode.com/problems/detect-capital) | Easy | [![Java](assets/java.png)](src/DetectCapital.java) [![Python](assets/python.png)](python/detect_capital.py) |
141141
| 521 | [Longest Uncommon Subsequence I](https://leetcode.com/problems/longest-uncommon-subsequence-i) | Easy | |
142142
| 530 | [Minimum Absolute Difference in BST](https://leetcode.com/problems/minimum-absolute-difference-in-bst) | Easy | |
143143
| 532 | [K - Diff Pairs in Array](https://leetcode.com/problems/k-diff-pairs-in-an-array) | Easy | |

python/detect_capital.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def detectCapitalUse(self, word: str) -> bool:
3+
return word.isupper() or word.islower() or word[0].isupper() and self.isLower(word, 1)
4+
5+
def isLower(self, word: str, start: int) -> bool:
6+
for index in range(start, len(word)):
7+
if word[index].isupper():
8+
return False
9+
return True

src/DetectCapital.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
public class DetectCapital {
2+
public boolean detectCapitalUse(String word) {
3+
return isAllCaps(word) || isAllLowerCase(word, 0) || firstCharIsCapital(word);
4+
}
5+
6+
private boolean isAllCaps(String string) {
7+
for (int index = 0 ; index < string.length() ; index++) {
8+
if (Character.isLowerCase(string.charAt(index))) {
9+
return false;
10+
}
11+
}
12+
return true;
13+
}
14+
15+
private boolean isAllLowerCase(String string, int start) {
16+
for (int index = start ; index < string.length() ; index++) {
17+
if (Character.isUpperCase(string.charAt(index))) {
18+
return false;
19+
}
20+
}
21+
return true;
22+
}
23+
24+
private boolean firstCharIsCapital(String string) {
25+
return Character.isUpperCase(string.charAt(0)) && isAllLowerCase(string, 1);
26+
}
27+
}

0 commit comments

Comments
 (0)