// array = [orange, [apple, [pear,[orange,apple]]], [watermelon,[lemon,apple]]], targetWord, k = 2 // => [ [apple, pear,[apple]], [watermelon, [lemon, apple]]] // // class NestedArray { // List<NestedArray> array; // String value; // // public boolean isEmpty() { return array.isEmpty() } // // [ orange, [apple, [pear]]] /* [ array1 => size is 2 orange, => string value is orange [ array2 => size is 2 apple, => string value is apple [ array3 => size is 1 pear => string value is pear ] ] ] */ // To execute C#, please define "static void Main" on a class // named Solution. class Solution { public class Node { List<Node> items; // either it is Node or string public string value {get; set;} public bool isString {get; set;} // Node next; // left, right -> public Node() { node1 = new Node(); node1.isString = true; node1.value = "orange"; node1B = new Node(); node1B.isString = true; node1B.value = "apple"; node2 = new Node(); node2.isString = false; node2.items = new List<Node>(); node2.Add(node1B); node2.Add(); // skip -> it is list simlar to node node1.next = node2; // written by interviewer - start from here // [ orange, [apple, [pear]]] NestedArray A; NestedArray B => B.isString = true, B.value = "orange"; NestedArray C => C.isString = false; NestedArray D => D.isString = true, D.value = "apple"; NestedArray E => E.isString = true, E.value = "pear"; NestedArray F => F.isString = false; F.arrays = [E]; NestedArray G => G.arrays = [D, F]; A.arrays = [B, G] // end of the code written by interviewer } // interviewer wrote the code in the following public void print(NestedArray a) { if (nestedArray.isString) { System.out.println(nestedArray.value); } else { System.out.println("["); for (NestedArray b : a.arrays) { print(b); } System.out.println("]"); } }