15 Jun 2024C++ / Python / TypeScriptEasy

Detect Capital

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.

solution files

  • C++ detect-capital/synced-solution.cpp
  • Python detect-capital/synced-solution.py
  • TypeScript detect-capital/synced-solution.ts

Solution files

Pythondetect-capital/synced-solution.py
class Solution:
    def detectCapitalUse(self, word: str) -> bool:
        return word.isupper() or word.islower() or word[class="syntax-number">1:].islower()
C++detect-capital/synced-solution.cpp
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;
    }
};
TypeScriptdetect-capital/synced-solution.ts
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());
}