LeetCode 520. Detect Capital #91
quinnwencn
started this conversation in
General
Replies: 1 comment
-
AnalysisThere are three cases in which the usage of capitals is right:
Solution written in Cppclass Solution {
public:
bool detectCapitalUse(string word) {
constexpr char MAX = 'Z';
if (word[0] <= MAX) { // start with Uppercase
if (word.length() == 1) {
return true;
}
bool isLowercase = word[1] > MAX;
for (auto i = 1; i < word.length(); ++i) {
if (word[i] <= MAX && isLowercase) {
return false;
} else if (word[i] > MAX && !isLowercase) {
return false;
}
}
} else {
for (auto i = 1; i < word.length(); ++i) {
if (word[i] <= MAX) {
return false;
}
}
}
return true;
}
};Solution written in Rustimpl Solution {
pub fn detect_capital_use(word: String) -> bool {
if word == word.to_uppercase() || word == word.to_lowercase() {
return true;
}
if word.chars().skip(1).all(|c| c.is_lowercase()) {
return true;
}
false
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Description
We define the usage of capitals in a word to be right when one of the following cases holds:
All letters in this word are capitals, like "USA".
All letters in this word are not capitals, like "leetcode".
Only the first letter in this word is capital, like "Google".
Given a string word, return true if the usage of capitals in it is right.
Example 1:
Input: word = "USA"
Output: true
Example 2:
Input: word = "FlaG"
Output: false
Constraints:
1 <= word.length <= 100
word consists of lowercase and uppercase English letters.
Beta Was this translation helpful? Give feedback.
All reactions