Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Essentially you can slide a window of the length of your word over your input and keep a count of how many of each letter are in the window. When the letter counts in your sliding window match the letter counts of your word, you have a match.</p> <p>Let your word length be <code>n</code>, and your current position be <code>curr</code>. Create an array, or <code>vector</code>, <code>windCounts</code> of length 26. The entry <code>windCounts[i]</code> stores the number of occurrences of the i<sup>th</sup> letter of the alphabet seen from position <code>curr - n - 1</code> to <code>curr</code>.</p> <p>What you do is you advance <code>curr</code>, and keep your array <code>windCounts</code> up to date, by decrementing the letter that has dropped out of the back of the sliding window, and incrementing the letter count that has appeared in the front of the sliding window. (Obviously until <code>curr</code> > <code>n</code>, you only increment, you just build up your sliding window to the length of your word.) </p> <p>In C++, you can use a <code>vector</code> for the counts of letters in your word, and for the counts of letters in your sliding window and simply use <code>vector::operator==</code> to do the equality.</p> <p><em>Edit</em>: the algorithm is <code>O(N)</code>, where <code>N</code> is the length of the text to search. This can be seen from the code below where the loop body is executed for each letter that you slide the window.</p> <pre><code>#include &lt;string&gt; #include &lt;vector&gt; #include &lt;algorithm&gt; // for_each using std::string; using std::vector; #include &lt;iostream&gt; int main(int argc, char* argv[]) { const string text = "forxxorfxdofr"; const string word = "for"; // Counts of letters in word vector&lt;int&gt; wordCounts(256); // optimization: cut down from 256 to 26 std::for_each(word.begin(), word.end(), [&amp;] (char c) { wordCounts[c]++; } ); // Current position of end of sliding window string::const_iterator curr = text.begin() + word.size(); // Initial sliding window counts vector&lt;int&gt; windCounts(256); std::for_each(text.begin(), curr, [&amp;] (char c) { windCounts[c]++; } ); // Run sliding window over text int numMatches = 0; while (1) { numMatches += wordCounts == windCounts; if (curr == text.end()) { break; } windCounts[*(curr - word.size())]--; windCounts[*curr]++; ++curr; } std::cout &lt;&lt; numMatches &lt;&lt; "\n"; return 0; } </code></pre>
    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