Skip to content

Commit 2c3d16d

Browse files
solves cows and bulls in python
1 parent 00676d3 commit 2c3d16d

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
| 290 | [Word Pattern](https://leetcode.com/problems/word-pattern) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/WordPattern.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/word_pattern.py) |
9090
| 292 | [Nim Game](https://leetcode.com/problems/nim-game) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/NimGame.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/nim_game.py) |
9191
| 293 | 🔒 [Flip Game](https://leetcode.com/problems/flip-game) | Easy | |
92-
| 299 | [Bulls and Cows](https://leetcode.com/problems/bulls-and-cows) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/BullsAndCows.java) |
92+
| 299 | [Bulls and Cows](https://leetcode.com/problems/bulls-and-cows) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/BullsAndCows.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/bulls_and_cows.py) |
9393
| 303 | [Range Sum Query - Immutable](https://leetcode.com/problems/range-sum-query-immutable) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/RangeSumQueryImmutable.java) |
9494
| 326 | [Power of Three](https://leetcode.com/problems/power-of-three) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/PowerOfThree.java) |
9595
| 339 | [Nested List Weight Sum](https://leetcode.com/problems/nested-list-weight-sum) | Easy | |

Diff for: python/bulls_and_cows.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from string import digits
2+
from typing import Dict
3+
4+
5+
class Solution:
6+
def get_digit_frequency(self, number: str) -> Dict[str, int]:
7+
freq = {}
8+
for digit in number:
9+
freq[digit] = freq.get(digit, 0) + 1
10+
return freq
11+
12+
def getHint(self, secret: str, guess: str) -> str:
13+
bulls = {}
14+
frequency_secret = self.get_digit_frequency(secret)
15+
frequency_guess = self.get_digit_frequency(guess)
16+
for s, g in zip(secret, guess):
17+
if s == g:
18+
bulls[s] = bulls.get(s, 0) + 1
19+
total_bulls = sum(bulls.values())
20+
total_cows = 0
21+
for i in digits:
22+
total_cows += min(frequency_secret.get(i, 0), frequency_guess.get(i, 0)) - bulls.get(i, 0)
23+
return f'{total_bulls}A{total_cows}B'

0 commit comments

Comments
 (0)