0299.cpp (597B)
1 class Solution { 2 public: 3 string getHint(string secret, string guess) { 4 int n = secret.size(), cows = 0, bulls = 0; 5 unordered_map<char, int> ums; 6 for (int i = 0; i < n; i++) 7 if (guess[i] == secret[i]) 8 bulls++; 9 else 10 ums[secret[i]]++; 11 12 for (int i = 0; i < n; i++) { 13 if (guess[i] == secret[i]) 14 continue; 15 else if (ums[guess[i]] > 0) 16 cows++; 17 ums[guess[i]]--; 18 } 19 return to_string(bulls) + "A" + to_string(cows) + "B"; 20 } 21 };