L520. Detect Capital

class Solution:
    def isCaptial(self, char):
        if ord(char) >= 65 and ord(char) <= 90:
            return True
        else:
            return False

    def detectCapitalUse(self, word):
        """
        :type word: str
        :rtype: bool
        """
        if not word:
            return False

        if self.isCaptial(word[0]): #检查第一个大写
            #CAP, Cap, check if from any changes starting from 2nd element
            if len(word) <= 2:
                return True
            check = self.isCaptial(word[1])
            for i in range(2, len(word)):
                if self.isCaptial(word[i]) == check:
                    continue
                else:
                    return False
            return True

        else:
            #cap
            for i in range(1, len(word)):
                if self.isCaptial(word[i]):
                    return False
            return True

Last updated

Was this helpful?