Merged List from Sources and Prefixes
<p>I have been given this below problem in school, I have solved as per my understanding and solution is as below. Can someone please help me to give a better solution for the same. </p>

<p>Question: </p>

<blockquote>
  <p>Produce a software application that creates a filtered merging of two case-insensitive sorted lists. The first input list is designated as the Source list and the other as the Prefixes list. The application will produce a merged list containing items that come from the Source and Prefixes lists using the following algorithm: </p>
  
  <p>An item X is in the merged list if and only if one of the following is true: </p>
  
  <p>a) X is from the Source list and there is an item Y in the Prefixes list that is a case-insensitive string prefix for X. </p>
  
  <p>b) X is from the Prefixes list and there is no item in the Source list for which X is a case-insensitive string prefix. </p>
  
  <p>The completed merged list should be in the same sort order as the items in the original two lists. </p>
</blockquote>

<p>My Solution: </p>

<pre><code>public ArrayList&lt;String&gt; merge(List&lt;String&gt; srcList, List&lt;String&gt; preList) {
        // If Prefixes list is empty then there cannot be a new merge list
        if (preList.isEmpty()) {
            return null;
        }
        int i = 0, j = 0;
        int sourcesListSize = srcList.size();
        int prefixesListSize = preList.size();
        ArrayList&lt;String&gt; mergeList = new ArrayList&lt;String&gt;();
        // Loop through Sources list until end of the list is reached
        //ASSUMPTION: Both SourceList and PrefixList are already sorted.
        while (i &lt; sourcesListSize &amp;&amp; j&lt;prefixesListSize) {
            mergeList.add(preList.get(j).concat(srcList.get(i)));
            i++;
            j++;
        }
        // If Prefixes list still have items, then add it to mergeList
        while (j &lt; prefixesListSize) {
            mergeList.add(preList.get(j));
            j++;
        }
        return mergeList;

    }
</code></pre>

<p>Input: </p>

<ul>
<li>Source list: <code>{"pple","ow","enver",pic,"ull"}</code></li>
<li>PrefixList: <code>{"a","c","d","e","f"}</code></li>
</ul>

<p><code>MergeList={"apple",cow","denver","epic","full"}</code></p>

<p>Is my understanding correct? Is there any best other solution? </p>
