Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Clear recursion solution in Clear category for Count Morse by CDG.Axel
D = {"a": ".-", "b": "-...", "c": "-.-.", "d": "-..", "e": ".", "f": "..-.", "g": "--.", "h": "....",
"i": "..", "j": ".---", "k": "-.-", "l": ".-..", "m": "--", "n": "-.", "o": "---", "p": ".--.",
"q": "--.-", "r": ".-.", "s": "...", "t": "-", "u": "..-", "v": "...-", "w": ".--", "x": "-..-",
"y": "-.--", "z": "--.."}
def morse_rec(msg: str, let: set):
return sum(morse_rec(msg[len(D[sym]):], let - {sym}) for sym in let if msg.startswith(D[sym])) if let else 1
def count_morse(message: str, letters: str) -> int:
# this solution doesn't work because max test has 1 307 674 368 000 permutations
# return sum(''.join(map(D.get, word)) == message for word in itertools.permutations(letters))
return morse_rec(message, set(letters))
May 16, 2023
Comments: