KatInterview

Split-or-Enumerate Symbol Sequence Decoder

Dynamic ProgrammingAuto-graded · 27 test cases
MediumAuto-gradedDynamic Programmingstringsdynamic-programmingbacktracking

Last updated

Tests string parsing, memoized backtracking, and combinatorial enumeration by decoding a symbol-encoded message with either explicit or missing letter boundaries.

Split-or-Enumerate Symbol Sequence Decoder

You are given a codebook: a dictionary mapping symbol patterns (strings of . and -) to single lowercase letters. You are also given an encoded message string, a letter delimiter letter_sep, and a word delimiter word_sep.

Your function must handle two distinct decoding modes depending on whether letter_sep is empty or not.

Mode 1 — Explicit letter boundaries (letter_sep is non-empty)

When letter_sep is provided, every letter boundary inside a word is marked by letter_sep, and word boundaries are marked by word_sep. Decode the message as follows:

  1. Split encoded on word_sep to recover individual encoded words. If word_sep is empty, treat the entire string as one word.
  2. Within each word, split on letter_sep to recover individual symbol patterns.
  3. Look up every pattern in the codebook. If any pattern is absent, the message is undecodable — return [].
  4. Otherwise, join the decoded letters into words, join the words with single spaces, and return that one sentence inside a list.

Mode 2 — Missing letter boundaries (letter_sep is empty)

When letter_sep is empty, words are still separated by word_sep, but there is no delimiter between letters inside a word. A single encoded word may be partitioned into valid patterns in multiple ways, producing multiple possible decoded words.

For each encoded word, find every way to segment it into a sequence of patterns that are all present in the codebook, such that the patterns concatenate exactly to that word. Combine the per-word sets of decodings into full sentences by taking the Cartesian product across all words (preserving word order) and joining with single spaces.

If any word has zero valid segmentations, the entire message is undecodable — return [].

Ordering rule: when exploring segmentations inside a word, try patterns in order of decreasing length; break ties alphabetically. This makes the output order deterministic.

Space joining rule: when building sentences from the product, a space is inserted between words only when the accumulated prefix is non-empty (so a leading empty-string word does not produce a leading space).

Duplicates: do not deduplicate. If two distinct segmentation paths produce the same letter sequence, that sequence appears multiple times in the output.

Return value

Always return a List[str]:

  • Mode 1 returns a list containing exactly one sentence, or [] on failure.
  • Mode 2 returns all valid sentences (possibly more than one), or [] if none exist.
  • If encoded is "", return [""] immediately (one valid decoding: the empty sentence).

Constraints

  • encoded consists of the characters ., -, and any separator characters; it may be empty.
  • letter_sep and word_sep are strings; either or both may be empty.
  • The codebook contains between 1 and 26 entries; every key is a non-empty string of . and -; every value is a single lowercase letter.
  • No codebook key contains a separator character.
  • When letter_sep is non-empty it is different from word_sep.
  • The encoded message length is at most 200 characters (excluding separators).
  • The function must return results in the deterministic order described above.

Examples

Example 1

Input: decode_symbol_message({'-.': 'n', '-': 't', '..': 'i', '.': 'e'}, '-. -/-.', ' ', '/')

Output: ['nt n']

Example 2

Input: decode_symbol_message({'-.': 'n', '-': 't', '..': 'i', '.': 'e'}, '-. -', '', '/')

Output: []

Example 3

Input: decode_symbol_message({'...': 's', '..': 'i', '.': 'e', '-': 't'}, '... ..', '', '/')

Output: []

Hints

Hint 1

In Mode 1 every boundary is marked, so two plain string splits plus dictionary lookups are enough — no search required. Focus first on getting that path right before thinking about Mode 2.

Hint 2

Mode 2 is the classic 'Word Break II' problem applied independently to each encoded word. Define a recursive function decode_from(i) that returns all letter strings decodable from position i to the end of the word, and memoize it on i so each suffix is solved only once.

Hint 3

After solving each word independently, the full set of sentences is the Cartesian product of the per-word result lists. Build it by iterating through the word results one at a time, extending each accumulated prefix. Remember to reset the memoization cache between words.