Advertisement
HjHimansh

Guess The Word

Aug 28th, 2020
1,568
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * // This is the Master's API interface.
  3.  * // You should not implement it, or speculate about its implementation
  4.  * class Master {
  5.  *   public:
  6.  *     int guess(string word);
  7.  * };
  8.  */
  9. class Solution {
  10.    
  11.    
  12. int overlap(string s1, string s2){
  13.   // always -> s1.length = s2.length = 6;
  14.   //return the number of overlapping letters in s1 and s2
  15.   int overlapLength = 0;
  16.   for(int i = 0; i<=5; i++){
  17.     if(s1[i] == s2[i]){
  18.       overlapLength++;
  19.     }
  20.   }
  21.   return overlapLength;
  22. }
  23.        
  24. public:
  25.     void findSecretWord(vector<string>& wordList, Master& master) {
  26.         unordered_set<string> words;
  27.         for(auto &x: wordList){
  28.             words.insert(x);
  29.         }
  30.        
  31.         while(!words.empty()){
  32.             string guessedWord = *(words.begin());
  33.            
  34.             int x = master.guess(guessedWord);
  35.             //cout << guessedWord << " - " << x << endl;
  36.             if(x==6){
  37.                 return;
  38.             }
  39.             words.erase(guessedWord);
  40.             if(x == 0){
  41.                 unordered_set<string> dup(words.begin(), words.end());
  42.                 for(auto &word: dup){
  43.                     if(overlap(word, guessedWord) > 0){
  44.                         words.erase(word);
  45.                     }
  46.                 }
  47.             }
  48.             else if(x > 0){
  49.                 unordered_set<string> dup(words.begin(), words.end());
  50.                 for(auto &word: dup){
  51.                     if(overlap(word, guessedWord) < x){
  52.                         words.erase(word);
  53.                     }
  54.                 }
  55.             }
  56.         }
  57.     }
  58. };
  59.  
  60.  
  61.  
  62. // Suppose we guess a random word r:
  63. //     x = master.guess(r);
  64. //     if(x == 0){
  65. //         //cross out all the words that have overlapLength > 0 with r...
  66. //         //guesss agian
  67. //     }
  68. //     if(x > 0){
  69. //         //cross out all the words that have overlapLength < x with r;
  70. //     }
Advertisement
RAW Paste Data Copied
Advertisement