from typing import Counter


def evaluate(solution, guess):
    """ Returns a tuple of a boolean and a hint string.

    The boolean is `True` iff the guessed word is the solution

    Hint string encoding, for each letter:\n
    `R` Right : Good letter in the good place.\n
    `M` Misplaced : Good letter in a bad place.\n
    `W` Wrong : Letter used too many times"""
    wordlength = len(solution)

    # Obvious cases handling :
    if len(guess) != wordlength or guess[0] != solution[0]:
        return False, 'W' * wordlength

    if guess == solution:
        return True, 'R' * wordlength

    # Other cases, initialize with "all wrong" hint list
    hints = ['W'] * wordlength

    # First pass: fill-up right guesses
    letters = Counter(solution)
    for i in range(len(solution)):
        right_letter = solution[i]
        guessed_letter = guess[i]
        if guessed_letter == right_letter:
            hints[i] = 'R'
            letters[guessed_letter] = letters[guessed_letter] - 1

    # Second pass: fill-up misplaced guesses
    for i in range(len(solution)):
        right_letter = solution[i]
        guessed_letter = guess[i]

        if hints[i] == 'R':
            continue

        if guessed_letter in list(letters) and letters[guessed_letter] > 0:
            hints[i] = 'M'
            letters[guessed_letter] = letters[guessed_letter] - 1

    hints = ''.join(hints)  # Convert hint list to hint string
    return False, hints