leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit 0287843fd2dee4052bd8546e745cfc049e0e3e52
parent 02f0bc8b11a97cb8429c00c7937c855fdb3419f8
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Thu,  2 Feb 2023 15:56:23 +0100

LeetCode 75 I: Day 13

Diffstat:
AProblems/0299.cpp | 21+++++++++++++++++++++
MREADME.md | 1+
2 files changed, 22 insertions(+), 0 deletions(-)

diff --git a/Problems/0299.cpp b/Problems/0299.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + string getHint(string secret, string guess) { + int n = secret.size(), cows = 0, bulls = 0; + unordered_map<char, int> ums; + for (int i = 0; i < n; i++) + if (guess[i] == secret[i]) + bulls++; + else + ums[secret[i]]++; + + for (int i = 0; i < n; i++) { + if (guess[i] == secret[i]) + continue; + else if (ums[guess[i]] > 0) + cows++; + ums[guess[i]]--; + } + return to_string(bulls) + "A" + to_string(cows) + "B"; + } +}; diff --git a/README.md b/README.md @@ -150,6 +150,7 @@ for solving problems. | 0287 | Medium | [Find the Duplicate Number](Problems/0287.cpp) | | 0290 | Easy | [Word Pattern](Problems/0290.cpp) | | 0295 | Hard | [Find Median from Data Stream](Problems/0295.cpp) | +| 0299 | Medium | [Bulls and Cows](Problems/0299.cpp) | | 0300 | Medium | [Longest Increasing Subsequence](Problems/0300.cpp) | | 0309 | Medium | [Best Time to Buy and Sell Stock with Cooldown](Problems/0309.cpp) | | 0310 | Medium | [Minimum Height Trees](Problems/0310.cpp) |