[리트코드] 299. Bulls and Cows

박형진·2023년 1월 2일
0

https://leetcode.com/problems/bulls-and-cows/description/


1. 코드

class Solution:
    def getHint(self, secret: str, guess: str) -> str:
        x = y = 0
        d = defaultdict(int)
        length = len(secret)
        """
        secret = '1123'
        guess = '0111'
        황소를 제외한 숫자의 개수를 d에 저장한다.
        
        print(d) ->
        defaultdict(<class 'int'>, {'1': 1, '2': 1, '3': 1})

		"""
        for i in range(length):
            if secret[i] == guess[i]:
                x += 1  # 황소 카운트
                continue
            else:
                d[secret[i]] += 1


        for i in range(length):
            if guess[i] not in d or secret[i] == guess[i]:
                continue
            if d[guess[i]] >= 1:
                d[guess[i]] -= 1
                y += 1

        return str(x) + 'A' + str(y) + 'B'
profile
안녕하세요!

0개의 댓글