forked from KnowledgeCenterYoutube/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path520_Detect_Capital
More file actions
54 lines (46 loc) · 1.45 KB
/
Copy path520_Detect_Capital
File metadata and controls
54 lines (46 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
Leetcode 520: Detect Capital
Detailed video explanation: https://youtu.be/qNJYp8aOpHQ
================================================
C++:
----
class Solution {
public:
bool detectCapitalUse(string word) {
if(word.length() < 2) return true;
if(isupper(word[0]) && isupper(word[1])){
for(int i = 2; i < word.length(); ++i)
if(islower(word[i])) return false;
} else {
for(int i = 1; i < word.length(); ++i)
if(isupper(word[i])) return false;
}
return true;
}
};
Java:
-----
class Solution {
public boolean detectCapitalUse(String word) {
if(word.length() < 2) return true;
if(Character.isUpperCase(word.charAt(0)) && Character.isUpperCase(word.charAt(1))){
for(int i = 2; i < word.length(); ++i)
if(Character.isLowerCase(word.charAt(i))) return false;
} else {
for(int i = 1; i < word.length(); ++i)
if(Character.isUpperCase(word.charAt(i))) return false;
}
return true;
}
}
Python3:
-------
class Solution:
def detectCapitalUse(self, word: str) -> bool:
if len(word) < 2: return True
if word[0].isupper() and word[1].isupper():
for i in range(2, len(word)):
if word[i].islower(): return False
else:
for i in range(1, len(word)):
if word[i].isupper(): return False
return True