Skip to content

feat: add solutions to lc problem: No.0299 #2427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 66 additions & 36 deletions solution/0200-0299/0299.Bulls and Cows/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,33 @@

## 解法

### 方法一
### 方法一:计数

我们创建两个计数器 $cnt1$ 和 $cnt2$,分别用来统计秘密数字和朋友猜测的数字中每个数字出现的次数。同时,我们创建一个变量 $x$ 来统计公牛的数量。

然后我们遍历秘密数字和朋友猜测的数字,如果当前数字相同,我们就将 $x$ 加一。否则,我们分别将秘密数字和朋友猜测的数字中的当前数字的计数加一。

最后,我们遍历 $cnt1$ 中的每个数字,取 $cnt1$ 和 $cnt2$ 中当前数字的计数的最小值,然后将这个最小值加到变量 $y$ 中。

最后,我们返回 $x$ 和 $y$ 的值。

时间复杂度 $O(n)$,其中 $n$ 是秘密数字和朋友猜测的数字的长度。空间复杂度 $O(|\Sigma|)$,其中 $|\Sigma|$ 是字符集的大小,本题中字符集为数字,所以 $|\Sigma| = 10$。

<!-- tabs:start -->

```python
class Solution:
def getHint(self, secret: str, guess: str) -> str:
x = y = 0
cnt1 = [0] * 10
cnt2 = [0] * 10
for i in range(len(secret)):
if secret[i] == guess[i]:
cnt1, cnt2 = Counter(), Counter()
x = 0
for a, b in zip(secret, guess):
if a == b:
x += 1
else:
cnt1[int(secret[i])] += 1
cnt2[int(guess[i])] += 1

for i in range(10):
y += min(cnt1[i], cnt2[i])
return f'{x}A{y}B'
cnt1[a] += 1
cnt2[b] += 1
y = sum(min(cnt1[c], cnt2[c]) for c in cnt1)
return f"{x}A{y}B"
```

```java
Expand Down Expand Up @@ -108,18 +115,20 @@ class Solution {
public:
string getHint(string secret, string guess) {
int x = 0, y = 0;
vector<int> cnt1(10);
vector<int> cnt2(10);
int cnt1[10]{};
int cnt2[10]{};
for (int i = 0; i < secret.size(); ++i) {
int a = secret[i] - '0', b = guess[i] - '0';
if (a == b)
if (a == b) {
++x;
else {
} else {
++cnt1[a];
++cnt2[b];
}
}
for (int i = 0; i < 10; ++i) y += min(cnt1[i], cnt2[i]);
for (int i = 0; i < 10; ++i) {
y += min(cnt1[i], cnt2[i]);
}
return to_string(x) + "A" + to_string(y) + "B";
}
};
Expand All @@ -128,24 +137,46 @@ public:
```go
func getHint(secret string, guess string) string {
x, y := 0, 0
cnt1 := make([]int, 10)
cnt2 := make([]int, 10)
for i := 0; i < len(secret); i++ {
a, b := secret[i]-'0', guess[i]-'0'
cnt1 := [10]int{}
cnt2 := [10]int{}
for i, c := range secret {
a, b := int(c-'0'), int(guess[i]-'0')
if a == b {
x++
} else {
cnt1[a]++
cnt2[b]++
}
}
for i := 0; i < 10; i++ {
y += min(cnt1[i], cnt2[i])
for i, c := range cnt1 {
y += min(c, cnt2[i])

}
return fmt.Sprintf("%dA%dB", x, y)
}
```

```ts
function getHint(secret: string, guess: string): string {
const cnt1: number[] = Array(10).fill(0);
const cnt2: number[] = Array(10).fill(0);
let x: number = 0;
for (let i = 0; i < secret.length; ++i) {
if (secret[i] === guess[i]) {
++x;
} else {
++cnt1[+secret[i]];
++cnt2[+guess[i]];
}
}
let y: number = 0;
for (let i = 0; i < 10; ++i) {
y += Math.min(cnt1[i], cnt2[i]);
}
return `${x}A${y}B`;
}
```

```php
class Solution {
/**
Expand All @@ -154,23 +185,22 @@ class Solution {
* @return String
*/
function getHint($secret, $guess) {
$cntA = 0;
$cntB = 0;
$len = strlen($secret);
for ($i = 0; $i < $len; $i++) {
if ($secret[$i] == $guess[$i]) {
$cntA++;
$cnt1 = array_fill(0, 10, 0);
$cnt2 = array_fill(0, 10, 0);
$x = 0;
for ($i = 0; $i < strlen($secret); ++$i) {
if ($secret[$i] === $guess[$i]) {
++$x;
} else {
$hashtable[$secret[$i]] += 1;
++$cnt1[(int) $secret[$i]];
++$cnt2[(int) $guess[$i]];
}
}
for ($i = 0; $i < $len; $i++) {
if ($secret[$i] != $guess[$i] && $hashtable[$guess[$i]] > 0) {
$cntB++;
$hashtable[$guess[$i]] -= 1;
}
$y = 0;
for ($i = 0; $i < 10; ++$i) {
$y += min($cnt1[$i], $cnt2[$i]);
}
return $cntA . 'A' . $cntB . 'B';
return "{$x}A{$y}B";
}
}
```
Expand Down
102 changes: 66 additions & 36 deletions solution/0200-0299/0299.Bulls and Cows/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,33 @@ Note that only one of the two unmatched 1s is counted as a cow since the non-bul

## Solutions

### Solution 1
### Solution 1: Counting

We create two counters, $cnt1$ and $cnt2$, to count the occurrence of each digit in the secret number and the friend's guess respectively. At the same time, we create a variable $x$ to count the number of bulls.

Then we iterate through the secret number and the friend's guess. If the current digit is the same, we increment $x$ by one. Otherwise, we increment the count of the current digit in the secret number and the friend's guess respectively.

Finally, we iterate through each digit in $cnt1$, take the minimum count of the current digit in $cnt1$ and $cnt2$, and add this minimum value to the variable $y$.

In the end, we return the values of $x$ and $y$.

The time complexity is $O(n)$, where $n$ is the length of the secret number and the friend's guess. The space complexity is $O(|\Sigma|)$, where $|\Sigma|$ is the size of the character set. In this problem, the character set is digits, so $|\Sigma| = 10$.

<!-- tabs:start -->

```python
class Solution:
def getHint(self, secret: str, guess: str) -> str:
x = y = 0
cnt1 = [0] * 10
cnt2 = [0] * 10
for i in range(len(secret)):
if secret[i] == guess[i]:
cnt1, cnt2 = Counter(), Counter()
x = 0
for a, b in zip(secret, guess):
if a == b:
x += 1
else:
cnt1[int(secret[i])] += 1
cnt2[int(guess[i])] += 1

for i in range(10):
y += min(cnt1[i], cnt2[i])
return f'{x}A{y}B'
cnt1[a] += 1
cnt2[b] += 1
y = sum(min(cnt1[c], cnt2[c]) for c in cnt1)
return f"{x}A{y}B"
```

```java
Expand Down Expand Up @@ -103,18 +110,20 @@ class Solution {
public:
string getHint(string secret, string guess) {
int x = 0, y = 0;
vector<int> cnt1(10);
vector<int> cnt2(10);
int cnt1[10]{};
int cnt2[10]{};
for (int i = 0; i < secret.size(); ++i) {
int a = secret[i] - '0', b = guess[i] - '0';
if (a == b)
if (a == b) {
++x;
else {
} else {
++cnt1[a];
++cnt2[b];
}
}
for (int i = 0; i < 10; ++i) y += min(cnt1[i], cnt2[i]);
for (int i = 0; i < 10; ++i) {
y += min(cnt1[i], cnt2[i]);
}
return to_string(x) + "A" + to_string(y) + "B";
}
};
Expand All @@ -123,24 +132,46 @@ public:
```go
func getHint(secret string, guess string) string {
x, y := 0, 0
cnt1 := make([]int, 10)
cnt2 := make([]int, 10)
for i := 0; i < len(secret); i++ {
a, b := secret[i]-'0', guess[i]-'0'
cnt1 := [10]int{}
cnt2 := [10]int{}
for i, c := range secret {
a, b := int(c-'0'), int(guess[i]-'0')
if a == b {
x++
} else {
cnt1[a]++
cnt2[b]++
}
}
for i := 0; i < 10; i++ {
y += min(cnt1[i], cnt2[i])
for i, c := range cnt1 {
y += min(c, cnt2[i])

}
return fmt.Sprintf("%dA%dB", x, y)
}
```

```ts
function getHint(secret: string, guess: string): string {
const cnt1: number[] = Array(10).fill(0);
const cnt2: number[] = Array(10).fill(0);
let x: number = 0;
for (let i = 0; i < secret.length; ++i) {
if (secret[i] === guess[i]) {
++x;
} else {
++cnt1[+secret[i]];
++cnt2[+guess[i]];
}
}
let y: number = 0;
for (let i = 0; i < 10; ++i) {
y += Math.min(cnt1[i], cnt2[i]);
}
return `${x}A${y}B`;
}
```

```php
class Solution {
/**
Expand All @@ -149,23 +180,22 @@ class Solution {
* @return String
*/
function getHint($secret, $guess) {
$cntA = 0;
$cntB = 0;
$len = strlen($secret);
for ($i = 0; $i < $len; $i++) {
if ($secret[$i] == $guess[$i]) {
$cntA++;
$cnt1 = array_fill(0, 10, 0);
$cnt2 = array_fill(0, 10, 0);
$x = 0;
for ($i = 0; $i < strlen($secret); ++$i) {
if ($secret[$i] === $guess[$i]) {
++$x;
} else {
$hashtable[$secret[$i]] += 1;
++$cnt1[(int) $secret[$i]];
++$cnt2[(int) $guess[$i]];
}
}
for ($i = 0; $i < $len; $i++) {
if ($secret[$i] != $guess[$i] && $hashtable[$guess[$i]] > 0) {
$cntB++;
$hashtable[$guess[$i]] -= 1;
}
$y = 0;
for ($i = 0; $i < 10; ++$i) {
$y += min($cnt1[$i], $cnt2[$i]);
}
return $cntA . 'A' . $cntB . 'B';
return "{$x}A{$y}B";
}
}
```
Expand Down
12 changes: 7 additions & 5 deletions solution/0200-0299/0299.Bulls and Cows/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ class Solution {
public:
string getHint(string secret, string guess) {
int x = 0, y = 0;
vector<int> cnt1(10);
vector<int> cnt2(10);
int cnt1[10]{};
int cnt2[10]{};
for (int i = 0; i < secret.size(); ++i) {
int a = secret[i] - '0', b = guess[i] - '0';
if (a == b)
if (a == b) {
++x;
else {
} else {
++cnt1[a];
++cnt2[b];
}
}
for (int i = 0; i < 10; ++i) y += min(cnt1[i], cnt2[i]);
for (int i = 0; i < 10; ++i) {
y += min(cnt1[i], cnt2[i]);
}
return to_string(x) + "A" + to_string(y) + "B";
}
};
13 changes: 7 additions & 6 deletions solution/0200-0299/0299.Bulls and Cows/Solution.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
func getHint(secret string, guess string) string {
x, y := 0, 0
cnt1 := make([]int, 10)
cnt2 := make([]int, 10)
for i := 0; i < len(secret); i++ {
a, b := secret[i]-'0', guess[i]-'0'
cnt1 := [10]int{}
cnt2 := [10]int{}
for i, c := range secret {
a, b := int(c-'0'), int(guess[i]-'0')
if a == b {
x++
} else {
cnt1[a]++
cnt2[b]++
}
}
for i := 0; i < 10; i++ {
y += min(cnt1[i], cnt2[i])
for i, c := range cnt1 {
y += min(c, cnt2[i])

}
return fmt.Sprintf("%dA%dB", x, y)
}
Loading