Leetcode 520 Solution

This article provides solution to leetcode question 520 (detect-capital)

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; } };