KatInterview

Unscramble a Cipher-Encoded Nested Outline

Stacks & QueuesAuto-graded · 18 test cases
MediumAuto-gradedStacks & Queuesstackstring-parsingtree-deserialization

Last updated

Parse a parenthesized pre-order tree string with a stack and undo a letter-substitution cipher, where each node's text is the run just before its closing parenthesis.

Unscramble a Cipher-Encoded Nested Outline

An outline is stored as a tree. Each node carries a short string of lowercase letters and spaces (it may be empty) and an ordered list of children.

The outline was written out in two stages:

  1. Letter substitution. A string cipher_key, which is a permutation of a-z, was used to scramble every letter. The letter at alphabet position i ('a' is 0) was replaced by cipher_key[i]. Spaces were not changed.

  2. Pre-order flattening. Each node was written as an opening parenthesis, then its scrambled text, then the flattened form of each child in order, then a closing parenthesis:

    "(" + scrambledText + child1 + child2 + ... + ")"

    Node texts never contain parentheses, so the parentheses mark node boundaries.

You are given cipher_key and encoded, the flattened string of exactly one root node. Rebuild the tree and return it as nested lists. Every node is a two-element list [text, children], where children is the list of the node's child nodes in their original order (empty for a leaf).

What goes into text

Scan encoded from left to right. The text of a node is the decoded run of non-parenthesis characters that sits directly before that node's own closing ):

  • For a leaf node, this is the whole decoded text written after its (. Spaces are kept exactly as they appear, including leading and trailing ones. An empty text stays "".
  • For a node that has at least one child, the characters written before its first child are not reported. Its text is "", because the run right before its closing ) is empty (that ) directly follows the last child's )).

In short, only leaves report their decoded text in the result, and every node with children has text equal to "".

Constraints

  • cipher_key has length 26 and is a permutation of the lowercase English letters.
  • 1 <= len(encoded) <= 10^5, and encoded is a valid flattening of exactly one root node.
  • The tree has at most 10^4 nodes.
  • Node texts contain only lowercase letters and spaces, and may be empty.
  • A solution that runs in time linear in len(encoded) is expected.
  • The tree may be a chain 10^4 nodes deep, so be careful with recursion.

Examples

Example 1

Input: decode_cipher_tree('bcdefghijklmnopqrstuvwxyza', '(ij(bc)(de))')

Output: ['', [['ab', []], ['cd', []]]]

Example 2

Input: decode_cipher_tree('qwertyuiopasdfghjklzxcvbnm', '(ug gf(xh(rgvf))())')

Output: ['', [['', [['down', []]]], ['', []]]]

Hints

Hint 1

Decoding is the inverse of the substitution: the character cipher_key[i] must map back to the letter at position i. Build this table once.

Hint 2

Scan the string once and keep a stack of nodes that have been opened but not yet closed. A new node is a child of whichever node is on top of the stack.

Hint 3

Keep a buffer of decoded characters. Clear it whenever you see a parenthesis. When you reach ')', assign the buffer as the text of the node on top of the stack, then pop that node.