520. Detect Capital ¶ Time: $O(n)$ Space: $O(1)$ C++JavaPython 1 2 3 4 5 6 7 8 9 10class Solution { public: bool detectCapitalUse(string word) { for (int i = 1; i < word.length(); ++i) if (isupper(word[1]) != isupper(word[i]) || islower(word[0]) && isupper(word[i])) return false; return true; } }; 1 2 3 4 5 6class Solution { public boolean detectCapitalUse(String word) { return word.equals(word.toUpperCase()) || word.substring(1).equals(word.substring(1).toLowerCase()); } } 1 2 3class Solution: def detectCapitalUse(self, word: str) -> bool: return word.isupper() or word.islower() or word.istitle()