-
Notifications
You must be signed in to change notification settings - Fork 0
/
299.py
53 lines (37 loc) · 1.09 KB
/
299.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = ['"wuyadong" <[email protected]>']
class Solution(object):
def getHint(self, secret, guess):
"""
:type secret: str
:type guess: str
:rtype: str
"""
d = dict()
for i, c in enumerate(secret):
if c not in d:
d[c] = set()
d[c].add(i)
bulls = 0
cows = 0
i = 0
while i < len(guess):
if guess[i] == secret[i]:
if i in d[secret[i]]:
d[secret[i]].remove(i)
bulls += 1
i += 1
for key in d.keys():
d[key] = len(d[key])
i = 0
while i < len(guess):
if guess[i] != secret[i] and guess[i] in d and d[guess[i]] > 0:
d[guess[i]] -= 1
cows += 1
i += 1
return "%dA%dB" % (bulls, cows)
if __name__ == "__main__":
print Solution().getHint("1123", "0111")
print Solution().getHint("1122", "2211")
print Solution().getHint("1234", "0111")