auto-generated entry for Detect Capital. the solution files are available below.
solution files
- C++
detect-capital/synced-solution.cpp - Python
detect-capital/synced-solution.py - TypeScript
detect-capital/synced-solution.ts
Collected C++, Python, TypeScript solutions for detect capital. Add a dedicated write-up later if you want deeper notes.
auto-generated entry for Detect Capital. the solution files are available below.
detect-capital/synced-solution.cppdetect-capital/synced-solution.pydetect-capital/synced-solution.tsclass Solution:
def detectCapitalUse(self, word: str) -> bool:
return word.isupper() or word.islower() or word[class="syntax-number">1:].islower()
class Solution {
public:
bool detectCapitalUse(string word) {
bool allUpper = true;
bool allLower = true;
bool firstUpperRestLower = isupper(word[class="syntax-number">0]);
for (int i = class="syntax-number">0; i < static_cast<int>(word.size()); i++) {
allUpper &= isupper(word[i]);
allLower &= islower(word[i]);
if (i > class="syntax-number">0) {
firstUpperRestLower &= islower(word[i]);
}
}
return allUpper || allLower || firstUpperRestLower;
}
};
function detectCapitalUse(word: string): boolean {
return word === word.toUpperCase() ||
word === word.toLowerCase() ||
(word[class="syntax-number">0] === word[class="syntax-number">0].toUpperCase() && word.slice(class="syntax-number">1) === word.slice(class="syntax-number">1).toLowerCase());
}