Skip to content

Commit 0a0e27c

Browse files
committed
new problem
1 parent 2a01dfe commit 0a0e27c

File tree

3 files changed

+65
-24
lines changed

3 files changed

+65
-24
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@
393393
|703|[Kth Largest Element in a Stream](https://leetcode.com/problems/kth-largest-element-in-a-stream/)|[Java](leetcode/solution/src/KthLargestElementInAStream.java)|100||
394394
|771|[Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/)|[Java](leetcode/solution/src/JewelsAndStones.java)|100||
395395
|819|[Most Common Word](https://leetcode.com/problems/most-common-word/)|[Java](leetcode/solution/src/MostCommonWord.java)|85||
396+
|843|[Guess the Word](https://leetcode.com/problems/guess-the-word/)|[Java](leetcode/solution/src/GuessTheWord.java)|90||
396397
|844|[Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/)|[Java](leetcode/solution/src/BackspaceStringCompare.java)|100||
397398
|857|[Minimum Cost to Hire K Workers](https://leetcode.com/problems/minimum-cost-to-hire-k-workers/)|[Java](leetcode/solution/src/MinimumCostToHireKWorkers.java|70||
398399
|904|[Fruit Into Baskets](https://leetcode.com/problems/fruit-into-baskets/)|[Java](leetcode/solution/src/FruitIntoBaskets.java)|90||
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
import java.util.Random;
4+
5+
public class GuessTheWord {
6+
7+
public interface Master {
8+
int guess(String word);
9+
}
10+
11+
/**
12+
* 这题的意思是有一个word列表,取其中一个作为secret,现在让你来猜哪个是secret,最多猜10次,
13+
* 你每猜一个单词,就告诉你这个单词和secret匹配的字母数。
14+
* 这里的思路很直接,就是随机取一个单词开始,获得了匹配的字母数后在单词列表中找到可能的候选项
15+
* 然后再迭代一次,这样猜10次
16+
*/
17+
public void findSecretWord(String[] wordlist, Master master) {
18+
Random random = new Random();
19+
for (int i = 0; i < 10; i++) {
20+
String word = wordlist[random.nextInt(wordlist.length)];
21+
int match = master.guess(word);
22+
List<String> list = new ArrayList<>();
23+
for (String s : wordlist) {
24+
if (match(s, word) == match) {
25+
list.add(s);
26+
}
27+
}
28+
wordlist = list.toArray(new String[0]);
29+
}
30+
}
31+
32+
private int match(String s, String t) {
33+
int match = 0;
34+
for (int i = 0; i < s.length(); i++) {
35+
if (s.charAt(i) == t.charAt(i)) {
36+
match++;
37+
}
38+
}
39+
return match;
40+
}
41+
}

leetcode/src/Main.java

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,35 @@
55
public class Main {
66

77
public static class Solution {
8-
public boolean backspaceCompare(String S, String T) {
9-
if (S.length() < T.length()) {
10-
return backspaceCompare(T, S);
11-
}
12-
Stack<Character> stack1 = new Stack<>();
13-
Stack<Character> stack2 = new Stack<>();
14-
for (int i = 0; i < S.length(); i++) {
15-
helper(stack1, S, i);
16-
helper(stack2, T, i);
17-
}
18-
while (!stack1.isEmpty() && !stack2.isEmpty()) {
19-
if (!stack1.pop().equals(stack2.pop())) {
20-
return false;
8+
9+
public interface Master {
10+
int guess(String word);
11+
}
12+
13+
14+
public void findSecretWord(String[] wordlist, Master master) {
15+
Random random = new Random();
16+
for (int i = 0; i < 10; i++) {
17+
String word = wordlist[random.nextInt(wordlist.length)];
18+
int match = master.guess(word);
19+
List<String> list = new ArrayList<>();
20+
for (String s : wordlist) {
21+
if (match(s, word) == match) {
22+
list.add(s);
23+
}
2124
}
25+
wordlist = list.toArray(new String[0]);
2226
}
23-
return stack1.isEmpty() && stack2.isEmpty();
2427
}
2528

26-
private void helper(Stack<Character> stack, String s, int i) {
27-
if (i >= s.length()) {
28-
return;
29-
}
30-
char c = s.charAt(i);
31-
if (c == '#') {
32-
if (!stack.isEmpty()) {
33-
stack.pop();
29+
private int match(String s, String t) {
30+
int match = 0;
31+
for (int i = 0; i < s.length(); i++) {
32+
if (s.charAt(i) == t.charAt(i)) {
33+
match++;
3434
}
35-
} else {
36-
stack.push(c);
3735
}
36+
return match;
3837
}
3938
}
4039

0 commit comments

Comments
 (0)