-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path299 Bulls and Cows.py
39 lines (31 loc) · 1.21 KB
/
299 Bulls and Cows.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#299 Bulls and Cows
class Solution:
def getHint(self, secret: str, guess: str) -> str:
a, b = 0, 0
N = len(secret)
secret_use = [False]*N
guess_use = [False]*N
for index, (answer,g) in enumerate(zip(secret,guess)):
if answer == g:
secret_use[index] = guess_use[index] = True
a += 1
for i in range(N):
if not secret_use[i]:
for j in range(N):
if not guess_use[j] and secret[i] == guess[j]:
b += 1
secret_use[i] = guess_use[j] = True
break
return f"{a}A{b}B"
# class Solution:
# def getHint(self, secret: str, guess: str) -> str:
# bull=0
# for i in range(len(secret)):
# bull += int(secret[i] == guess[i])
# # This loop will take care of "cow" cases
# cows=0
# # print(set(secret))
# for c in set(secret):
# print(c, secret.count(c),guess.count(c))
# cows += min(secret.count(c), guess.count(c))
# return f"{bull}A{cows-bull}B"