/** * // 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{public:voidfindSecretWord(vector<string>&wordlist,Master&master){srand(time(nullptr));// Requiredfor(inti=0;i<10;++i){conststring&guessedWord=wordlist[rand()%wordlist.size()];constintmatches=master.guess(guessedWord);if(matches==6)break;vector<string>updated;for(conststring&word:wordlist)if(getMatches(guessedWord,word)==matches)updated.push_back(word);wordlist=move(updated);}}private:intgetMatches(conststring&s1,conststring&s2){intmatches=0;for(inti=0;i<s1.length();++i)if(s1[i]==s2[i])++matches;returnmatches;}};
/** * // This is the Master's API interface. * // You should not implement it, or speculate about its implementation * interface Master { * public int guess(String word) {} * } */classSolution{publicvoidfindSecretWord(String[]wordlist,Mastermaster){Randomrand=newRandom();for(inti=0;i<10;++i){finalStringguessedWord=wordlist[rand.nextInt(wordlist.length)];finalintmatches=master.guess(guessedWord);if(matches==6)break;List<String>updated=newArrayList<>();for(finalStringword:wordlist)if(getMatches(guessedWord,word)==matches)updated.add(word);wordlist=updated.toArray(newString[0]);}}privateintgetMatches(finalStrings1,finalStrings2){intmatches=0;for(inti=0;i<s1.length();++i)if(s1.charAt(i)==s2.charAt(i))++matches;returnmatches;}}
# """# This is Master's API interface.# You should not implement it, or speculate about its implementation# """# Class Master:# def guess(self, word: str) -> int:classSolution:deffindSecretWord(self,wordlist:List[str],master:'Master')->None:defgetMatches(s1:str,s2:str)->int:matches=0forc1,c2inzip(s1,s2):ifc1==c2:matches+=1returnmatchesfor_inrange(10):guessedWord=wordlist[randint(0,len(wordlist)-1)]matches=master.guess(guessedWord)ifmatches==6:breakwordlist=[wordforwordinwordlistifgetMatches(guessedWord,word)==matches]