An alchemist builds a potion using a blending tree. Every leaf of the tree is a single named ingredient, written as one lowercase letter a-z. Every internal node is a blender: it always has exactly two children, and it simply pours the two children's contents together, so its output amount is the sum of its left child's amount and its right child's amount. Because pouring together is symmetric and doesn't care about grouping, the only thing that determines a blending tree's total potency, for a fixed potency value assigned to each ingredient letter, is how many times each letter appears among the tree's leaves.
The same ingredient letter may appear at more than one leaf of a tree (meaning that ingredient gets poured in more than once), and different trees may use completely different shapes to combine the same ingredients.
Two blending trees are called equivalent if, for every possible way of assigning a positive potency value to each of the 26 ingredient letters, the two trees always yield exactly the same total potency. Given two blending trees, decide whether they are equivalent.
Each tree is described as a sequence of tokens listed in preorder (a node's token comes before its children's tokens). A token is either the uppercase letter B, meaning "this is a blender node — its left subtree's tokens come next, followed immediately by its right subtree's tokens", or a single lowercase letter, meaning "this is a leaf holding that ingredient". Every B token is followed, in the token stream, by exactly two complete subtrees, so the token list unambiguously reconstructs one full binary tree per input tree.
Four lines:
n1, the number of tokens describing the first tree.n1 space-separated tokens (each either B or a single lowercase letter), the preorder token list of the first tree.n2, the number of tokens describing the second tree.n2 space-separated tokens, the preorder token list of the second tree.Print EQUIVALENT if the two trees yield the same total potency for every possible assignment of potency values to the 26 ingredient letters, otherwise print NOT EQUIVALENT.
B has exactly two children); consequently n1 and n2 are always odd.a-z; the same letter may appear any number of times, in either tree.Example 1
Input
5 B B a b c 5 B a B b c
Expected
EQUIVALENT
Explanation
Tree 1 first blends a and b, then adds c: it uses a, b, and c exactly once each. Tree 2 first blends b and c, then adds a: it also uses a, b, and c exactly once each. Since a blender's output never depends on the order or grouping of the additions, both trees produce the same total potency for any potency values assigned to a, b, and c, so the answer is EQUIVALENT.
Example 2
Input
3 B a a 3 B a b
Expected
NOT EQUIVALENT
Explanation
Tree 1 blends the ingredient a with itself, using letter a twice and letter b zero times. Tree 2 blends a with b, using each once. The per-letter counts differ (a:2,b:0 versus a:1,b:1), so assigning, say, potency 1 to a and 5 to b makes tree 1 total 2 while tree 2 totals 6 — the trees are NOT EQUIVALENT.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →