2025-07-30 11:49:28 +0000 UTC

Detect Capital

Code

class Solution:
    def detectCapitalUse(self, word: str) -> bool:
        length = len(word)
        if length == 1:
            return True
        def is_cap(char: str) -> bool:
            res = 65 <= ord(char) <= 90
            return res
        first_cap, second_cap = is_cap(word[0]), is_cap(word[1])
        if first_cap and second_cap:
            should_be_cap = True
        elif first_cap and not second_cap:
            should_be_cap = False
        elif not first_cap and second_cap:
            return False
        elif not first_cap and not second_cap:
            should_be_cap = False
        else:
            raise Exception
        for char in word[2:]:
            if is_cap(char) != should_be_cap:
                return False
        return True