Python solution with detailed explanation


  • 2
    G

    Solution

    Detect Capital https://leetcode.com/problems/detect-capital/?tab=Description

    class Solution(object):
        def detectCapitalUse(self, word):
            """
            :type word: str
            :rtype: bool
            """
            x = 0
            for ch in word:
                if 65<=ord(ch)<=96:
                    x = x+1
            if len(word) == x or x == 0:
                return True
            if len(word) > 1 and x == 1 and 65<=ord(word[0])<=96:
                return True
            return False
    

  • 0

    The and x == 1 in if len(word) > 1 and x == 1 and 65<=ord(word[0])<=96: is not needed, because a single capital char should be True.


  • 0
    G

    @FrankChen0130

    A single capital letter is not necessarily always true. Given the case "gooGle" there is one single capital letter but this would be returned false. We need to check to make sure the single capital letter is at the beginning of the word.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.