Detect Capital

easy string

Problem

Return true if the usage of capitals in word is right: all letters uppercase (USA), all lowercase (leetcode), or only the first letter capital (Google).

Inputword = "USA"
Outputtrue
All letters are uppercase.

def detect_capital_use(word):
    return word.isupper() or word.islower() or word.istitle()
function detectCapitalUse(word) {
  if (word === word.toUpperCase()) return true;
  if (word === word.toLowerCase()) return true;
  return word[0] === word[0].toUpperCase() &&
         word.slice(1) === word.slice(1).toLowerCase();
}
class Solution {
    public boolean detectCapitalUse(String word) {
        return word.equals(word.toUpperCase())
            || word.equals(word.toLowerCase())
            || (Character.isUpperCase(word.charAt(0))
                && word.substring(1).equals(word.substring(1).toLowerCase()));
    }
}
bool detectCapitalUse(string word) {
    int upper = 0;
    for (char c : word) if (isupper(c)) upper++;
    return upper == (int)word.size() || upper == 0
        || (upper == 1 && isupper(word[0]));
}
Time: O(n) Space: O(1)