ปานกลาง 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

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 กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

คำตอบที่ผ่านดีที่สุดของแต่ละคน คำตอบที่เร็วกว่า ใช้หน่วยความจำน้อยกว่า และได้คะแนนดีกว่าจะอยู่ด้านบน

ยังไม่มีคำตอบที่ผ่าน แก้โจทย์นี้แล้วคว้าอันดับหนึ่งไปเลย

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก