Skip to content

Commit fbacdb6

Browse files
committed
feat: add solutions to lcci problem: No.16.15
No.16.15.Master Mind
1 parent b2d0ecf commit fbacdb6

File tree

7 files changed

+211
-4
lines changed

7 files changed

+211
-4
lines changed

lcci/16.15.Master Mind/README.md

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,99 @@
2323

2424
<!-- 这里可写通用的实现逻辑 -->
2525

26+
**方法一:哈希表**
27+
28+
同时遍历两个字符串,算出对应位置字符相同的个数,累加到 $x$ 中,然后将两个字符串出现的字符以及出现的次数分别记录在哈希表 `cnt1``cnt2` 中。
29+
30+
接着遍历两个哈希表,算出有多少共同出现的字符,累加到 $y$ 中。那么答案就是 $[x, y - x]$。
31+
32+
时间复杂度 $O(C)$,空间复杂度 $O(C)$。本题中 $C=4$。
33+
2634
<!-- tabs:start -->
2735

2836
### **Python3**
2937

3038
<!-- 这里可写当前语言的特殊实现逻辑 -->
3139

3240
```python
33-
41+
class Solution:
42+
def masterMind(self, solution: str, guess: str) -> List[int]:
43+
x = sum(a == b for a, b in zip(solution, guess))
44+
y = sum((Counter(solution) & Counter(guess)).values())
45+
return [x, y - x]
3446
```
3547

3648
### **Java**
3749

3850
<!-- 这里可写当前语言的特殊实现逻辑 -->
3951

4052
```java
53+
class Solution {
54+
public int[] masterMind(String solution, String guess) {
55+
int x = 0, y = 0;
56+
Map<Character, Integer> cnt1 = new HashMap<>();
57+
Map<Character, Integer> cnt2 = new HashMap<>();
58+
for (int i = 0; i < 4; ++i) {
59+
char a = solution.charAt(i), b = guess.charAt(i);
60+
x += a == b ? 1 : 0;
61+
cnt1.put(a, cnt1.getOrDefault(a, 0) + 1);
62+
cnt2.put(b, cnt2.getOrDefault(b, 0) + 1);
63+
}
64+
for (char c : "RYGB".toCharArray()) {
65+
y += Math.min(cnt1.getOrDefault(c, 0), cnt2.getOrDefault(c, 0));
66+
}
67+
return new int[] {x, y - x};
68+
}
69+
}
70+
```
71+
72+
### **C++**
73+
74+
```cpp
75+
class Solution {
76+
public:
77+
vector<int> masterMind(string solution, string guess) {
78+
int x = 0, y = 0;
79+
unordered_map<char, int> cnt1;
80+
unordered_map<char, int> cnt2;
81+
for (int i = 0; i < 4; ++i) {
82+
x += solution[i] == guess[i];
83+
cnt1[solution[i]]++;
84+
cnt2[guess[i]]++;
85+
}
86+
for (char c : "RYGB") y += min(cnt1[c], cnt2[c]);
87+
return vector<int>{x, y - x};
88+
}
89+
};
90+
```
4191
92+
### **Go**
93+
94+
```go
95+
func masterMind(solution string, guess string) []int {
96+
var x, y int
97+
cnt1 := map[byte]int{}
98+
cnt2 := map[byte]int{}
99+
for i := range solution {
100+
a, b := solution[i], guess[i]
101+
if a == b {
102+
x++
103+
}
104+
cnt1[a]++
105+
cnt2[b]++
106+
}
107+
for _, c := range []byte("RYGB") {
108+
y += min(cnt1[c], cnt2[c])
109+
}
110+
return []int{x, y - x}
111+
}
112+
113+
func min(a, b int) int {
114+
if a < b {
115+
return a
116+
}
117+
return b
118+
}
42119
```
43120

44121
### **JavaScript**

lcci/16.15.Master Mind/README_EN.md

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,82 @@
3333
### **Python3**
3434

3535
```python
36-
36+
class Solution:
37+
def masterMind(self, solution: str, guess: str) -> List[int]:
38+
x = sum(a == b for a, b in zip(solution, guess))
39+
y = sum((Counter(solution) & Counter(guess)).values())
40+
return [x, y - x]
3741
```
3842

3943
### **Java**
4044

4145
```java
46+
class Solution {
47+
public int[] masterMind(String solution, String guess) {
48+
int x = 0, y = 0;
49+
Map<Character, Integer> cnt1 = new HashMap<>();
50+
Map<Character, Integer> cnt2 = new HashMap<>();
51+
for (int i = 0; i < 4; ++i) {
52+
char a = solution.charAt(i), b = guess.charAt(i);
53+
x += a == b ? 1 : 0;
54+
cnt1.put(a, cnt1.getOrDefault(a, 0) + 1);
55+
cnt2.put(b, cnt2.getOrDefault(b, 0) + 1);
56+
}
57+
for (char c : "RYGB".toCharArray()) {
58+
y += Math.min(cnt1.getOrDefault(c, 0), cnt2.getOrDefault(c, 0));
59+
}
60+
return new int[] {x, y - x};
61+
}
62+
}
63+
```
64+
65+
### **C++**
66+
67+
```cpp
68+
class Solution {
69+
public:
70+
vector<int> masterMind(string solution, string guess) {
71+
int x = 0, y = 0;
72+
unordered_map<char, int> cnt1;
73+
unordered_map<char, int> cnt2;
74+
for (int i = 0; i < 4; ++i) {
75+
x += solution[i] == guess[i];
76+
cnt1[solution[i]]++;
77+
cnt2[guess[i]]++;
78+
}
79+
for (char c : "RYGB") y += min(cnt1[c], cnt2[c]);
80+
return vector<int>{x, y - x};
81+
}
82+
};
83+
```
4284
85+
### **Go**
86+
87+
```go
88+
func masterMind(solution string, guess string) []int {
89+
var x, y int
90+
cnt1 := map[byte]int{}
91+
cnt2 := map[byte]int{}
92+
for i := range solution {
93+
a, b := solution[i], guess[i]
94+
if a == b {
95+
x++
96+
}
97+
cnt1[a]++
98+
cnt2[b]++
99+
}
100+
for _, c := range []byte("RYGB") {
101+
y += min(cnt1[c], cnt2[c])
102+
}
103+
return []int{x, y - x}
104+
}
105+
106+
func min(a, b int) int {
107+
if a < b {
108+
return a
109+
}
110+
return b
111+
}
43112
```
44113

45114
### **JavaScript**

lcci/16.15.Master Mind/Solution.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
vector<int> masterMind(string solution, string guess) {
4+
int x = 0, y = 0;
5+
unordered_map<char, int> cnt1;
6+
unordered_map<char, int> cnt2;
7+
for (int i = 0; i < 4; ++i) {
8+
x += solution[i] == guess[i];
9+
cnt1[solution[i]]++;
10+
cnt2[guess[i]]++;
11+
}
12+
for (char c : "RYGB") y += min(cnt1[c], cnt2[c]);
13+
return vector<int>{x, y - x};
14+
}
15+
};

lcci/16.15.Master Mind/Solution.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
func masterMind(solution string, guess string) []int {
2+
var x, y int
3+
cnt1 := map[byte]int{}
4+
cnt2 := map[byte]int{}
5+
for i := range solution {
6+
a, b := solution[i], guess[i]
7+
if a == b {
8+
x++
9+
}
10+
cnt1[a]++
11+
cnt2[b]++
12+
}
13+
for _, c := range []byte("RYGB") {
14+
y += min(cnt1[c], cnt2[c])
15+
}
16+
return []int{x, y - x}
17+
}
18+
19+
func min(a, b int) int {
20+
if a < b {
21+
return a
22+
}
23+
return b
24+
}

lcci/16.15.Master Mind/Solution.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int[] masterMind(String solution, String guess) {
3+
int x = 0, y = 0;
4+
Map<Character, Integer> cnt1 = new HashMap<>();
5+
Map<Character, Integer> cnt2 = new HashMap<>();
6+
for (int i = 0; i < 4; ++i) {
7+
char a = solution.charAt(i), b = guess.charAt(i);
8+
x += a == b ? 1 : 0;
9+
cnt1.put(a, cnt1.getOrDefault(a, 0) + 1);
10+
cnt2.put(b, cnt2.getOrDefault(b, 0) + 1);
11+
}
12+
for (char c : "RYGB".toCharArray()) {
13+
y += Math.min(cnt1.getOrDefault(c, 0), cnt2.getOrDefault(c, 0));
14+
}
15+
return new int[] {x, y - x};
16+
}
17+
}

lcci/16.15.Master Mind/Solution.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution:
2+
def masterMind(self, solution: str, guess: str) -> List[int]:
3+
x = sum(a == b for a, b in zip(solution, guess))
4+
y = sum((Counter(solution) & Counter(guess)).values())
5+
return [x, y - x]

solution/1700-1799/1781.Sum of Beauty of All Substrings/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@
4545

4646
**方法一:枚举 + 计数**
4747

48-
枚举每个子串的起点,找到以该起点为左端点的所有子串,然后计算每个子串的美丽值,最后将所有子串的美丽值相加即可
48+
枚举每个子串的起点位置 $i$,找到以该起点位置的字符为左端点的所有子串,然后计算每个子串的美丽值,累加到答案中
4949

50-
时间复杂度 O(n^2 \times C),空间复杂度 $O(C)$。其中 $n$ 为字符串的长度,而 $C$ 为字符集的大小。本题中 $C = 26$。
50+
时间复杂度 $O(n^2 \times C)$,空间复杂度 $O(C)$。其中 $n$ 为字符串的长度,而 $C$ 为字符集的大小。本题中 $C = 26$。
5151

5252
<!-- tabs:start -->
5353

0 commit comments

Comments
 (0)