Guess the Word

hard array math game theory interactive

Problem

You're given a list of 6-letter words and an interactive Master that reports the number of matching letter-positions between your guess and the hidden secret. Find the secret in ≤ 10 guesses.

Inputwordlist = ["acckzz","ccbazz","eiowzz","abcczz"], secret="acckzz"
Output"acckzz"
Each round, pick the candidate whose worst-case partition is smallest, then filter by the Master's feedback.

def findSecretWord(wordlist, master):
    def match(a, b):
        return sum(1 for x, y in zip(a, b) if x == y)
    candidates = wordlist[:]
    for _ in range(10):
        # pick word that minimizes worst-case 0-match group
        best, best_score = candidates[0], len(candidates)
        for w in candidates:
            zeros = sum(1 for c in candidates if match(w, c) == 0)
            if zeros < best_score:
                best_score, best = zeros, w
        m = master.guess(best)
        if m == 6: return
        candidates = [c for c in candidates if match(c, best) == m]
function findSecretWord(wordlist, master) {
  const match = (a, b) => { let m = 0; for (let i = 0; i < 6; i++) if (a[i] === b[i]) m++; return m; };
  let candidates = wordlist.slice();
  for (let round = 0; round < 10; round++) {
    let best = candidates[0], bestScore = candidates.length;
    for (const w of candidates) {
      let zeros = 0;
      for (const c of candidates) if (match(w, c) === 0) zeros++;
      if (zeros < bestScore) { bestScore = zeros; best = w; }
    }
    const m = master.guess(best);
    if (m === 6) return;
    candidates = candidates.filter(c => match(c, best) === m);
  }
}
class Solution {
    public void findSecretWord(String[] wordlist, Master master) {
        List<String> cands = new ArrayList<>(Arrays.asList(wordlist));
        for (int r = 0; r < 10; r++) {
            String best = cands.get(0); int bestScore = cands.size();
            for (String w : cands) {
                int zeros = 0;
                for (String c : cands) if (match(w, c) == 0) zeros++;
                if (zeros < bestScore) { bestScore = zeros; best = w; }
            }
            int m = master.guess(best);
            if (m == 6) return;
            final String guess = best; final int mm = m;
            List<String> next = new ArrayList<>();
            for (String c : cands) if (match(c, guess) == mm) next.add(c);
            cands = next;
        }
    }
    int match(String a, String b) { int m = 0; for (int i = 0; i < 6; i++) if (a.charAt(i) == b.charAt(i)) m++; return m; }
}
void findSecretWord(vector<string>& wordlist, Master& master) {
    auto match = [](const string& a, const string& b){ int m = 0; for (int i = 0; i < 6; i++) if (a[i] == b[i]) m++; return m; };
    vector<string> cands = wordlist;
    for (int r = 0; r < 10; r++) {
        string best = cands[0]; int bestScore = (int)cands.size();
        for (auto& w : cands) {
            int zeros = 0;
            for (auto& c : cands) if (match(w, c) == 0) zeros++;
            if (zeros < bestScore) { bestScore = zeros; best = w; }
        }
        int m = master.guess(best);
        if (m == 6) return;
        vector<string> next;
        for (auto& c : cands) if (match(c, best) == m) next.push_back(c);
        cands = next;
    }
}
Time: O(N² · rounds) Space: O(N)