Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Isn't it doable in O(n log n)?</p> <p>Step 1, reduce the string by eliminating all characters that appear to the right. Strictly speaking you only need to eliminate characters if they appear in the string you're checking.</p> <pre><code>/** Reduces the maximal subsequence of characters in container that contains no * character from container that appears to the left of the same character in * container. E.g. "common" -&gt; "cmon", and "whirlygig" -&gt; "whrlyig". */ static String reduceContainer(String container) { SparseVector charsToRight = new SparseVector(); // Like a Bitfield but sparse. StringBuilder reduced = new StringBuilder(); for (int i = container.length(); --i &gt;= 0;) { char ch = container.charAt(i); if (charsToRight.add(ch)) { reduced.append(ch); } } return reduced.reverse().toString(); } </code></pre> <p>Step 2, check containment.</p> <pre><code>static boolean containsInOrder(String container, String containee) { int containerIdx = 0, containeeIdx = 0; int containerLen = container.length(), containeeLen == containee.length(); while (containerIdx &lt; containerLen &amp;&amp; containeeIdx &lt; containeeLen) { // Could loop over codepoints instead of code-units, but you get the point... if (container.charAt(containerIdx) == containee.charAt(containeeIdx)) { ++containeeIdx; } ++containerIdx; } return containeeIdx == containeeLen; } </code></pre> <p>And to answer your second question, no, Levenshtein distance won't help you since it has the property that if you swap the arguments the output is the same, but the algo you want does not.</p>
    singulars
    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. 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