O(n) python solution


  • 0
    H
    class Solution(object):
        def getHint(self, secret, guess):
            """
            :type secret: str
            :type guess: str
            :rtype: str
            """
            repo=[0]*10
            A=0
            B=0
            for i in range(len(secret)):
                if secret[i]==guess[i]:
                    A+=1
                else:
                    repo[int(secret[i])]+=1
            for i in range(len(secret)):
                if secret[i]!=guess[i] and repo[int(guess[i])]>0:
                    B+=1
                    repo[int(guess[i])]-=1
            
            return '%dA%dB'%(A,B)
    

Log in to reply
 

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