You are given an array of unique strings wordlist where wordlist[i] is 6 letters long, and one word in this list is chosen as secret.
You may call Master.guess(word) to guess a word. The guessed word should have type string and must be from the original list with 6 lowercase letters.
This function returns an integer type, representing the number of exact matches (value and position) of your guess to the secret word. Also, if your guess is not in the given wordlist, it will return -1 instead.
For each test case, you have exactly 10 guesses to guess the word. At the end of any number of calls, if you have made 10 or fewer calls to Master.guess and at least one of these guesses was secret, then you pass the test case.
/** * // This is the Master's API interface. * // You should not implement it, or speculate about its implementation * class Master { * public: * int guess(string word); * }; */ classSolution { intmatch(string& origin, string& target){ int count = 0; for(int i = 0; i < origin.length(); i++) { if(origin[i] == target[i]) count++; } return count; }
voidremoveUnMatchStrings(vector<string>& words, string& picked, int matchCountFromMaster){ for(int i = 0; i < words.size(); i++) { int matchCount = match(picked, words[i]); if(matchCount != matchCountFromMaster){ words.erase(words.begin() + i); i--; } } }
string pickOne(vector<string>& words){ int arr[6][26]{0}; for(auto& w: words) { for(int i = 0; i < 6; i++) { arr[i][w[i]-'a']++; } } int idx = 0, ch = 0, ma = 0; for(int i = 0; i < 6; i++) { int maxIdx, maxValue = 0; for(int j = 0; j < 26; j++) { if(arr[i][j] > maxValue) { maxIdx = j; maxValue = arr[i][j]; } } if(ma < maxIdx) { ma = maxIdx; ch = maxIdx; idx = i; } } for(auto& w : words) { if(w[idx]-'a' == ch) return w; } return""; }