Leetcode 520 Solution
This article provides solution to leetcode question 520 (detect-capital)
Access this page by simply typing in "lcs 520" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/detect-capital
Solution
class Solution {
public:
bool detectCapitalUse(string word) {
if (word.size() == 0)
return true;
bool capital = 'A' <= word[0] && word[0] <= 'Z';
if (capital == false)
{
for (int i = 1; i < word.size(); i++)
if ('A' <= word[i] && word[i] <= 'Z')
return false;
}
else if (word.size() > 2)
{
capital = 'A' <= word[1] && word[1] <= 'Z';
for (int i = 2; i < word.size(); i++)
if (capital && !('A' <= word[i] && word[i] <= 'Z'))
return false;
else if (capital == false && ('A' <= word[i] && word[i] <= 'Z'))
return false;
}
return true;
}
};