Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The original answer is correct for: is a string <code>a</code> a <em>substring</em> of <code>b</code> (misread).</p> <p>Using a trie, you can simply add all strings to it in a first iteration, and in the 2nd iteration, start reading each word, let it be <code>w</code>. If you find a word that you finished your read, but did not reach the string terminator (<code>$</code> usually), you reach some node <code>v</code> in the trie. <br>By doing a <a href="http://en.wikipedia.org/wiki/Depth-first_search" rel="nofollow">DFS</a> from <code>v</code>, you can get all strings which <code>w</code> is prefix of them.</p> <p>high level pseudo code:</p> <pre><code>t &lt;- new trie for each word w: t.add(w) for each word w: node &lt;- t.getLastNode(w) if node.val != $ collection&lt;- DFS(node) (excluding w itself) w is a prefix of each word in collection </code></pre> <p>Note: in order to optimize it, you might need to do some extra work: if <code>a</code> is prefix of <code>b</code>, and <code>b</code> is prefix of <code>c</code>, then <code>a</code> is prefix of <code>c</code>, so - when you do the DFS, if you reach some node that was already searched - just append its strings to the current prefix. <br>Still, since there could be quadric number of possibilities (<code>"a", "aa", "aaa", ....</code> ), getting all of them requires quadric time.</p> <hr> <p>Original answer: finding if <code>a</code> is a <em>substring</em> of <code>b</code>:</p> <p>The suggested solution runs in a quadric complexity, you will need to check each two pairs, giving you <code>O(n* (n-1) * |S|)</code>.</p> <p>You can build a <a href="http://en.wikipedia.org/wiki/Suffix_tree" rel="nofollow">suffix tree</a> from the strings in the first iteration, and in the 2nd iteration check if each string is a non trivial entry (not itself) of another string. <br>This solution is <code>O(n*|S|)</code></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. 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