Translate Values in a Nested Map
Problem statement
A nested map is represented as a rooted tree. Node 0 is the root. For each other node, parent[i] is its containing map node. A node with isLeaf[i] == true stores the string values[i]; a map node stores the empty string.
The supplied translations array contains [source, translated] pairs and represents an expensive converter. A leaf value is invalid when it ends with _id.
- If any leaf value is invalid, return the original
valuesarray unchanged. - Otherwise, translate every leaf while keeping every map-node value unchanged.
- Repeated equal leaf values must reuse one converter lookup.
Return the resulting values in the original node order.
Function
translateNestedValues(parent: int[], isLeaf: boolean[], values: String[], translations: String[][]) → String[]Examples
Example 1
parent = [-1,0,0]isLeaf = [false,true,true]values = ["","hello","world"]translations = [["hello","hola"],["world","mundo"]]return = ["","hola","mundo"]Both leaf values are valid, so both are translated. The root map node remains empty.
Example 2
parent = [-1,0,1,1]isLeaf = [false,false,true,true]values = ["","","hello","hello"]translations = [["hello","hola"]]return = ["","","hola","hola"]The repeated leaf value is looked up once and reused for both nested leaves.
Example 3
parent = [-1,0,0]isLeaf = [false,true,true]values = ["","hello","account_id"]translations = [["hello","hola"],["account_id","cuenta"]]return = ["","hello","account_id"]Because one leaf ends with _id, the operation is atomic and returns every original value.
Constraints
1 <= parent.length == isLeaf.length == values.length <= 100000.parent[0] == -1, and fori > 0,0 <= parent[i] < i.- Map nodes have an empty value; leaf nodes have a non-empty value.
0 <= translations.length <= 100000, and source strings are distinct.- Every distinct valid leaf value appears in
translations. - The combined length of all strings is at most
10^6.