|
5 | 5 | public class Main { |
6 | 6 |
|
7 | 7 | 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 | + } |
21 | 24 | } |
| 25 | + wordlist = list.toArray(new String[0]); |
22 | 26 | } |
23 | | - return stack1.isEmpty() && stack2.isEmpty(); |
24 | 27 | } |
25 | 28 |
|
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++; |
34 | 34 | } |
35 | | - } else { |
36 | | - stack.push(c); |
37 | 35 | } |
| 36 | + return match; |
38 | 37 | } |
39 | 38 | } |
40 | 39 |
|
|
0 commit comments