Tutte le sfide
Media 0 risolte 0% di accettazione

Hidden Word Clue

Build the clue for a word guessing game: exact letters, letters in the wrong spot, and misses.

Cosa imparerai

  • Compare two strings position by position
  • Use counts to handle repeated letters
  • Build a result string from characters

Problema

Mostra traduzione

In a classroom word game one player hides a word and the other guesses words of the same length. After each guess the game shows a clue with one character per position:

  • the letter itself if the guess has the right letter in that position;
  • + if the letter is in the hidden word but somewhere else;
  • * if it is not.

Repeated letters are handled by counting: first mark every exact match. The hidden word's letters that were not matched exactly are then available for + marks. Going through the guess from left to right, each non-exact letter takes one available copy of that letter and gets a +; when no copy is left it gets a *.

Implement hidden_word_clue(hidden, guess).

Input

hidden: The hidden word in uppercase letters.

guess: The guess in uppercase letters, the same length as hidden.

Output

Return the clue as a string with the same length as the words.

Vincoli

1 ≤ word length ≤ 20

Both words use only A-Z

Esempi

Esempio 1
Input:
hidden = "PLANT"
guess = "PANEL"

Output atteso:
expected = "P++*+"
Input:
std::string hidden = "PLANT";
std::string guess = "PANEL";

Output atteso:
std::string expected = "P++*+";
Input:
string hidden = "PLANT";
string guess = "PANEL";

Output atteso:
string expected = "P++*+";

Spiegazione

P is exact. A, N and L are in PLANT but in other places, and there is no E: P++*+.

Esempio 2
Input:
hidden = "APPLE"
guess = "PAPPY"

Output atteso:
expected = "++P**"
Input:
std::string hidden = "APPLE";
std::string guess = "PAPPY";

Output atteso:
std::string expected = "++P**";
Input:
string hidden = "APPLE";
string guess = "PAPPY";

Output atteso:
string expected = "++P**";

Spiegazione

The middle P (position 3) is exact. One more P is left in APPLE, so the first P gets + and the second extra P gets *. A is elsewhere, Y is missing: ++P**.

Come affrontarlo

Prova prima da solo, poi apri un suggerimento alla volta.

Suggerimento 1

Do two passes: exact matches first, then the rest.

Suggerimento 2

Count the hidden word's letters that were not exact matches.

Suggerimento 3

A `+` uses up one copy of its letter from those counts.

Complessità attesa

O(n) time, O(1) extra space

Riferimenti

Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.

La migliore soluzione superata di ogni programmatore. Le più veloci, leggere e meglio valutate stanno in alto.

Ancora nessuna soluzione superata. Risolvila e prendi il primo posto.

Impara dagli altri

Soluzioni della community

Nessuna soluzione pubblicata. Risolvi questa sfida per condividere la prima.