From d5d29c928abe9f04e39aae852f2eb9c17a2d62c1 Mon Sep 17 00:00:00 2001 From: sp995 <100511753+sp995@users.noreply.github.com> Date: Tue, 15 Oct 2024 13:41:19 +0530 Subject: [PATCH 1/2] Create BullsAndCows.java --- src/main/java/com/thealgorithms/others/BullsAndCows.java | 1 + 1 file changed, 1 insertion(+) create mode 100644 src/main/java/com/thealgorithms/others/BullsAndCows.java diff --git a/src/main/java/com/thealgorithms/others/BullsAndCows.java b/src/main/java/com/thealgorithms/others/BullsAndCows.java new file mode 100644 index 000000000000..8b137891791f --- /dev/null +++ b/src/main/java/com/thealgorithms/others/BullsAndCows.java @@ -0,0 +1 @@ + From 2bb205218438d0a13e508d21bdb5230b5e41e58d Mon Sep 17 00:00:00 2001 From: sp995 <100511753+sp995@users.noreply.github.com> Date: Tue, 15 Oct 2024 13:41:41 +0530 Subject: [PATCH 2/2] Update BullsAndCows.java --- .../thealgorithms/others/BullsAndCows.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/main/java/com/thealgorithms/others/BullsAndCows.java b/src/main/java/com/thealgorithms/others/BullsAndCows.java index 8b137891791f..463383da4525 100644 --- a/src/main/java/com/thealgorithms/others/BullsAndCows.java +++ b/src/main/java/com/thealgorithms/others/BullsAndCows.java @@ -1 +1,21 @@ +class BullsAndCows { + public String getHint(String secret, String guess) { + int A = 0; + int B = 0; + int[] count1 = new int[10]; + int[] count2 = new int[10]; + for (int i = 0; i < secret.length(); ++i) + if (secret.charAt(i) == guess.charAt(i)) + ++A; + else { + ++count1[secret.charAt(i) - '0']; + ++count2[guess.charAt(i) - '0']; + } + + for (int i = 0; i < 10; ++i) + B += Math.min(count1[i], count2[i]); + + return String.valueOf(A) + "A" + String.valueOf(B) + "B"; + } +}