Python recursive Solution (48 ms)


  • 0
    D

    '''

    def detectCapitalUse(self, word):
    
        if word[0].isupper():
            o=self.remaining_word(word[1:],[len(word[1:]),len(word[1:])])
            if o in [0,1]:
                final= True
            else: 
                final= False
        else:
            o=self.remaining_word(word[1:],[len(word[1:]),len(word[1:])] )
            if o in [0,2]:
                final= False
            else:
                final= True
        return final
    
    def remaining_word(self,word,list_a):
        if word =='':
            if list_a[1]==0:
                return 1
            if list_a[0]==0:
                return 0
    
            return 2
    
        if word[0].isupper():
            list_a[0]-=1
            return self.remaining_word(word[1:],list_a)
        else:
            list_a[1]-=1
            return self.remaining_word(word[1:],list_a)
    

    '''


Log in to reply
 

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