classSolution { public: booldetectCapitalUse(string& word){ bool small = true, big = true; for (int i = 1; i < word.size(); i++) { if (islower(word[i])) { big = false; } else { small = false; } } return small || (big && isupper(word[0])); } };
Go
1 2 3 4 5 6 7 8 9 10 11 12 13
// package main
// import "unicode"
funcdetectCapitalUse(word string)bool { cntLower := 0 for i := 1; i < len(word); i++ { if unicode.IsLower(rune(word[i])) { cntLower++ } } return cntLower == len(word) - 1 || cntLower == 0 && unicode.IsUpper(rune(word[0])) }