[InterviewBit] Bulls and Cows

Bulls and Cows

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
string Solution::solve(string A, string B) {
unordered_map<char, int> mp1, mp2;
int b = 0, c = 0;
for(int i = 0; i < A.size(); i++) {
if(A[i] == B[i]) b += 1;
else {
mp1[A[i]] += 1;
mp2[B[i]] += 1;
}
}
for(auto [k,v] : mp2) {
c += min(v,mp1[k]);
}
return to_string(b) + "A" + to_string(c) + "B";
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/11/15/PS/interviewbit/bulls-and-cows/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.