-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguesstheWord.cpp
More file actions
36 lines (34 loc) · 966 Bytes
/
guesstheWord.cpp
File metadata and controls
36 lines (34 loc) · 966 Bytes
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
// Source: https://leetcode.com/problems/guess-the-word/
// Author: Miao Zhang
// Date: 2021-03-17
/**
* // This is the Master's API interface.
* // You should not implement it, or speculate about its implementation
* class Master {
* public:
* int guess(string word);
* };
*/
class Solution {
public:
void findSecretWord(vector<string>& wordlist, Master& master) {
for (int i = 0, cnt = 0; i < 10 && cnt < 6; i++) {
string guess = wordlist[rand() % wordlist.size()];
cnt = master.guess(guess);
vector<string> words;
for (string& w: wordlist) {
if (match(guess, w) == cnt) {
words.push_back(w);
}
}
wordlist = words;
}
}
int match(string& a, string& b) {
int res = 0;
for (int i = 0; i < a.size(); i++) {
if (a[i] == b[i]) res++;
}
return res;
}
};