algorithms

interview · backtracking · medium

Morse decode — all combinations

You're handed a map from uppercase letters to Morse code and a string of dots and dashes with no separators. Return every English string that encodes to it.

The problem

Asked in a live coding screen, October 2026. Part one was the warm-up: encode an uppercase string to Morse using the given map, one lookup per letter. Part two is the real question. Morse has no separators here, so "-.." is D, but it's also T E E, N E, and T I. Return all of them. Order doesn't matter.

Signature: decodeMorse(morse, englishToMorse). The map is the standard 26-letter table, so every code is 1 to 4 symbols long. The input contains only . and -.

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: "--.."

Examples

input output
"-.." ["D", "NE", "TI", "TEE"]
"..." ["S", "EI", "IE", "EEE"]
"-.-" ["K", "NT", "TA", "TET"]
".-.-" ["RT", "AA", "EK", "AET", "ENT", "ETA", "ETET"]
"......" 29 strings, including "HI", "SS", "III", "EEEEEE"

Closest LeetCode: 140. Word Break II, with the Morse codes as the dictionary. The count-only version is the shape of 91. Decode Ways.

Why it branches

The tempting first idea is a two-pointer scan: at index i, check chunks of length 1, 2, 3, 4, then move to i + 1. That scan finds every letter that could start at each index. What it can't do is chain them. A decoding is a sequence of chunks where each one starts exactly where the previous one ended, so which position you continue from depends on which chunk you picked. One pointer can't be in three places at once.

That dependency is the recursion. Backtracking: at i, for each valid chunk, push its letter, recurse from the chunk's end, pop. Reaching the end of the string means the path is one complete answer. Invalid chunks are skipped immediately, which is what makes this far cheaper than generating every cut and filtering.

The same idea runs bottom-up as DP: walk right to left and let dp[i] hold every decoding of the suffix from i. For "list them all" backtracking wins on memory. For "how many" or "is there one", dp[i] becomes a number and the whole thing is O(n).

Sandbox

Write the function, run it against the hidden cases. Cmd or Ctrl + Enter runs, Tab indents, Esc then Tab leaves the editor. The map is passed in as the second argument. 0 / 2 solved.

1. Decode every combination

Return every English string that encodes to the input. Any order.

Example
decodeMorse("-..", englishToMorse)
→ ["D", "NE", "TI", "TEE"]

2. Follow-up: just count them

Return how many English strings encode to the input. Aim for O(n) with a DP table of counts.

Example
countDecodings("-..", englishToMorse)
→ 4

Complexity

approach time space
brute force · every cut, then filterO(2ⁿ · n)O(n)
backtracking · list all k decodingsO(k · n)O(n) + output
DP · list all k decodingsO(k · n)O(k · n)
DP · count onlyO(n)O(n)

n is the Morse string length, k the number of decodings. Branching is capped at 4 per position, and k can be exponential in n, which no algorithm that lists them all can avoid.

Edge cases