-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution843.cs
66 lines (56 loc) · 1.68 KB
/
Solution843.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution843
{
/// <summary>
/// 843. Guess the Word - Hard
/// <a href="https://leetcode.com/problems/guess-the-word">See the problem</a>
/// </summary>
public void FindSecretWord(string[] words, Master master)
{
int n = words.Length;
// Function to calculate the number of exact matches between two words
int Match(string word1, string word2)
{
int matches = 0;
for (int i = 0; i < word1.Length; i++)
{
if (word1[i] == word2[i])
{
matches++;
}
}
return matches;
}
var candidates = new List<string>(words);
for (int attempts = 0; attempts < 10; attempts++)
{
// Choose a random word (or apply heuristics for better selection)
var guess = candidates[new Random().Next(candidates.Count)];
// Call Master.guess and get feedback
var matches = master.Guess(guess);
if (matches == 6)
{
return; // Found the secret word
}
// Filter candidates based on feedback
var nextCandidates = new List<string>();
foreach (string word in candidates)
{
if (Match(word, guess) == matches)
{
nextCandidates.Add(word);
}
}
candidates = nextCandidates;
}
}
public class Master
{
public int Guess(string word)
{
return 0;
}
}
}