Rearrange a list of items in an array based on different patterns using Javascript
<p>I recently wrote a similar question, but removed it because the question itself was not clear and sufficiently descriptive. So here goes:</p>

<p>I’m trying to rearrange a list of items in an array based on different patterns. For example, let’s say we have an array that looks like this:</p>

<pre><code>var Items = ['l','w','m','m','w','h','l','w','m','m','w','h','l','w','h'];
</code></pre>

<p>Now, I would like to rearrange the items to map any of these patterns:</p>

<pre><code>var Patterns = ['l', 'ww', 'hh' , 'wmm', 'mmw', 'hmm', 'mmh', 'mmmm'];
</code></pre>

<p>I don’t want to pick the items randomly, I would like to keep the chronological hierarchy of the items as close as possible, reading from left to right. Also, the pattern array is prioritized reading from left to right. ('l' is more important than 'ww' and so on.)</p>

<h3>I’ll try to describe the process, step by step:</h3>

<p>If we use the above Items array, we would like to check the first item: Items[0] and compare it to the Patterns array. </p>

<p>So:</p>

<p><strong>Step 1.</strong> Items[0] (l) matches Patterns[0] (<strong>l</strong>) perfectly, so stop searching for more pattern combination and save this and continue from here.</p>

<pre><code>Output: l, …
</code></pre>

<p><strong>Step 2.</strong> Items[1] (w) matches Patterns[1] (<strong>w</strong> w) and Patterns[3] (<strong>w</strong> mm), continue searching since all pattern combinations that starts with or includes a 'w' is combined with another character. </p>

<p><strong>Step 3</strong> <em>(continuing from Step 2)</em>. Items[1] (w) + Items[2] (m) matches Patterns[3] (<strong>wm</strong> m). Continue since Patterns[3] includes 3 characters (wmm)</p>

<p><strong>Step 4</strong> <em>(continuing from Step 3)</em>. Items[1] (w) + Items[2] (m) + Items[3] (m) matches Patterns[3] (<strong>wmm</strong>). Close and save since it's a perfect match.</p>

<pre><code>Output: l, w, m, m, …
</code></pre>

<p><strong>Step 5.</strong> Items[4] (w) matches Patterns[1] (<strong>w</strong> w) and Patterns[3] (<strong>w</strong> mm), continue searching since all pattern combinations that starts with or includes a 'w' is combined with another character. </p>

<p><strong>Step 6.</strong> <em>(continuing from Step 5)</em>. Items[5] (h) does not match any pattern combinations starting with 'w' (previous step) so: grab and move the nearest 'w' which in this case is Items[7] and combine with Items[4] (previous step) since it's a match of Patterns[1] (<strong>ww</strong>) and Patterns[1] is more important (prioritized) than Patterns[3] because we read the array from left to right.</p>

<pre><code>Output: l, w, m, m, w, w, …
</code></pre>

<p>Continue... </p>

<hr>

<p>There might be impossible to combine the last or the two characters from the Items array, not sure. Here is the complete output (if my counting is correct) of the above Items array after steping thru all combinations:</p>

<pre><code>Output: l, w, m, m, w, w, h, h, l, m, m, w, l, w, h
</code></pre>

<p>Do anyone know how this might be done using Javascript?</p>
