Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can avoid recursion here. Just walk the array. Compare the current array element to the string you are accumulating. When you see a new "main" element for the first time, append it to the string you are building. When you see a closing instance of a main element that matches the suffix of the string you have so far, pop it. Build up the resulting array as you walk.</p> <p>For example, given</p> <pre><code>["one", "foo", "two", "baz", "two", "one", "three", "lulz", "wtf", "three"] </code></pre> <p>You first see "one" so emit that</p> <pre><code>result = ["one"] working = "one" </code></pre> <p>Then you see <code>"foo"</code> so emit, but keep only <code>"one"</code> as the string to compare against.</p> <pre><code>result = ["one", "one.foo"] working = "one" </code></pre> <p>Next you have <code>"two"</code> so emit and keep <code>"one.two"</code>. </p> <pre><code>result = ["one", "one.foo", "one.two"] working = "one.two" </code></pre> <p>Next is <code>"baz"</code> emit but do not keep.</p> <pre><code>result = ["one", "one.foo", "one.two", "one.two.baz"] working = "one.two" </code></pre> <p>Next is a <code>"two"</code> so you can pop!</p> <pre><code>result = ["one", "one.foo", "one.two", "one.two.baz"] working = "one" </code></pre> <p>Next, a <code>"one"</code> so pop again!</p> <pre><code>result = ["one", "one.foo", "one.two", "one.two.baz"] working = "" </code></pre> <p>Now a three, which is a main tag, so emit and keep:</p> <pre><code>result = ["one", "one.foo", "one.two", "one.two.baz", "three"] working = "three" </code></pre> <p>Straightforward! I think you can take it from here. No fancy recursion is needed. Of course you may have to check for errors in nesting and balancing.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload