Logo
    Logo

    ©️ 2020-2026, Akhil Abraham.

    LinkedInGitHubMediumX
    Akhil Abraham
    Akhil Abraham
    /Posts
    Posts
    /LeetCode
    LeetCode
    /
    LeetCode
    /0520 - Detect Capital
    0520 - Detect Capital
    0520 - Detect Capital
    0520 - Detect Capital

    0520 - Detect Capital

    Difficulty
    Easy
    Language
    Python
    URL
    https://leetcode.com/problems/detect-capital/

    Solution 1 - Built-in String Methods

    Given a string word, return True if the usage of capitals is right. Valid cases: all uppercase ("USA"), all lowercase ("leetcode"), or only the first letter capitalized ("Google"). Use Python's built-in string methods to check each case directly.

    TimeComplexity:O(n)TimeComplexity: O(n)TimeComplexity:O(n)
    SpaceComplexity:O(n)SpaceComplexity: O(n)SpaceComplexity:O(n)

    Solution 2 - Count Uppercase Letters

    Count the number of uppercase letters in the word. The usage is correct if the count is 0 (all lowercase), equals the word length (all uppercase), or equals 1 and the first letter is uppercase (title case).

    TimeComplexity:O(n)TimeComplexity: O(n)TimeComplexity:O(n)
    SpaceComplexity:O(1)SpaceComplexity: O(1)SpaceComplexity:O(1)
    """ 0520.1 - Solution 1 - Built-in String Methods """
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def detectCapitalUse(self, word: str) -> bool:
            """Detect Capital Use Function"""
            return word.isupper() or word.islower() or word.istitle()
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().detectCapitalUse("USA"))       # True
        print(Solution().detectCapitalUse("leetcode"))  # True
        print(Solution().detectCapitalUse("Google"))    # True
        print(Solution().detectCapitalUse("FlaG"))      # False
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()
    
    """ 0520.2 - Solution 2 - Count Uppercase Letters """
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def detectCapitalUse(self, word: str) -> bool:
            """Detect Capital Use Function"""
            upper_count = sum(1 for c in word if c.isupper())
            return upper_count == 0 or upper_count == len(word) or (upper_count == 1 and word[0].isupper())
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().detectCapitalUse("USA"))       # True
        print(Solution().detectCapitalUse("leetcode"))  # True
        print(Solution().detectCapitalUse("Google"))    # True
        print(Solution().detectCapitalUse("FlaG"))      # False
        print(Solution().detectCapitalUse("gooGle"))    # False
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()