Tous les défis
Moyen 0 résolus 0 % de réussite

Hidden Word Clue

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

Ce que vous allez apprendre

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

Problème

Afficher la traduction

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.

Contraintes

1 ≤ word length ≤ 20

Both words use only A-Z

Exemples

Exemple 1
Entrée :
hidden = "PLANT"
guess = "PANEL"

Sortie attendue :
expected = "P++*+"
Entrée :
std::string hidden = "PLANT";
std::string guess = "PANEL";

Sortie attendue :
std::string expected = "P++*+";
Entrée :
string hidden = "PLANT";
string guess = "PANEL";

Sortie attendue :
string expected = "P++*+";

Explication

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

Exemple 2
Entrée :
hidden = "APPLE"
guess = "PAPPY"

Sortie attendue :
expected = "++P**"
Entrée :
std::string hidden = "APPLE";
std::string guess = "PAPPY";

Sortie attendue :
std::string expected = "++P**";
Entrée :
string hidden = "APPLE";
string guess = "PAPPY";

Sortie attendue :
string expected = "++P**";

Explication

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**.

Comment l'aborder

Essayez d'abord seul, puis ouvrez les indices un par un.

Indice 1

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

Indice 2

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

Indice 3

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

Complexité attendue

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

Références

Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.

La meilleure solution validée de chaque codeur. Les plus rapides, les plus légères et les mieux notées passent devant.

Aucune solution validée pour l'instant. Résolvez-le et prenez la première place.

Apprenez des autres

Solutions de la communauté

Aucune solution publiée pour l'instant. Résolvez ce défi pour partager la première.