Trung bình 0 đã giải Tỷ lệ chấp nhận 0%

Hidden Word Clue

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

Bạn sẽ học được gì

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

Đề bài

Xem bản dịch

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.

Ràng buộc

1 ≤ word length ≤ 20

Both words use only A-Z

Ví dụ

Ví dụ 1
Đầu vào:
hidden = "PLANT"
guess = "PANEL"

Kết quả mong đợi:
expected = "P++*+"
Đầu vào:
std::string hidden = "PLANT";
std::string guess = "PANEL";

Kết quả mong đợi:
std::string expected = "P++*+";
Đầu vào:
string hidden = "PLANT";
string guess = "PANEL";

Kết quả mong đợi:
string expected = "P++*+";

Giải thích

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

Ví dụ 2
Đầu vào:
hidden = "APPLE"
guess = "PAPPY"

Kết quả mong đợi:
expected = "++P**"
Đầu vào:
std::string hidden = "APPLE";
std::string guess = "PAPPY";

Kết quả mong đợi:
std::string expected = "++P**";
Đầu vào:
string hidden = "APPLE";
string guess = "PAPPY";

Kết quả mong đợi:
string expected = "++P**";

Giải thích

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

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

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

Gợi ý 2

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

Gợi ý 3

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

Độ phức tạp mong đợi

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

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Lời giải đạt tốt nhất của mỗi người. Lời giải nhanh hơn, tốn ít bộ nhớ hơn và được đánh giá cao hơn sẽ đứng trên.

Chưa có lời giải nào đạt. Giải bài này để giành hạng nhất.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.