中級 0件解決済み 正答率0%

Hidden Word Clue

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

このチャレンジで学べること

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

問題

翻訳を表示

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.

制約

1 ≤ word length ≤ 20

Both words use only A-Z

例

例 1
入力:
hidden = "PLANT"
guess = "PANEL"

期待される出力:
expected = "P++*+"
入力:
std::string hidden = "PLANT";
std::string guess = "PANEL";

期待される出力:
std::string expected = "P++*+";
入力:
string hidden = "PLANT";
string guess = "PANEL";

期待される出力:
string expected = "P++*+";

解説

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

例 2
入力:
hidden = "APPLE"
guess = "PAPPY"

期待される出力:
expected = "++P**"
入力:
std::string hidden = "APPLE";
std::string guess = "PAPPY";

期待される出力:
std::string expected = "++P**";
入力:
string hidden = "APPLE";
string guess = "PAPPY";

期待される出力:
string expected = "++P**";

解説

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

解き方のヒント

まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。

ヒント 1

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

ヒント 2

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

ヒント 3

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

目安の計算量

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

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

まだ合格した解答はありません。解いて 1 位になりましょう。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。