Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's the implementation that occurs to me.</p> <pre><code>//Implementing here with two List&lt;String&gt; //Should be easy enough to use arrays, or streams, or whatever. public static int getShortestSubseqWith(List&lt;String&gt; text, List&lt;String&gt; words) { int minDistance = Integer.MAX_VALUE; //Create a map of the last known position of each word Map&lt;String, Integer&gt; map = new HashMap(); for (String word : words) { map.put(word, -1); } String word; //One loop through the main search string for (int position = 0; position &lt; text.size(); position++){ word = text.get(position); //If the current word found is in the list we're looking for if (map.containsKey(word)) { //Update the map map.put(word, position); //And if the current positions are the closest seen so far, update the min value. int curDistance = getCurDistance(map); if (curDistance &lt; minDistance) minDistance = curDistance; } } return minDistance; } //Get the current distance between the last known position of each value in the map private static int getCurDistance(Map&lt;String, Integer&gt; map) { int min = Integer.MAX_VALUE; int max = 0; for (Integer value : map.values()) { if (value == -1) return Integer.MAX_VALUE; else { max = Math.max(max,value); min = Math.min(min,value); } } return max - min; } </code></pre> <p>The main performance influence here, if hits are relatively sparse, and list of terms to search for relatively small, should just be the loop over the <code>text</code> to be searched. If hits are very frequent, performance may suffer, due to more frequent runs through <code>getCurDistance</code>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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