Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm not sure this question is still of someone interest, but I have an idea that maybe could work.</p> <p>once we decided that the problem is not to find the substring, is to decide which letter is more convenient to remove from string A, the solution to me appears pretty simple: if you find an occurrence of B string into A, the best thing you can do is just remove a char that is inside the string, closed to the right bondary...let say the one previous the last. That's why if you have a substring that actually end how it starts, if you remove a char at the beginning you just remove one of the B occurencies, while you can actually remove two at once.</p> <p>Algorithm in pseudo cose:</p> <pre><code>String A, B; int occ_posit = 0; N = B.length(); occ_posit = A.getOccurrencePosition(B); // pseudo function that get the first occurence of B into A and returns the offset (1° byte = 1), or 0 if no occurence present. while (occ_posit &gt; 0) // while there are B into A { if (B.firstchar == B.lastchar) // if B starts as it ends { if (A.charat[occ_posit] == A.charat[occ_posit+1]) A.remove[occ_posit - 1]; // no reason to remove A[occ_posit] here else A.remove[occ_posit]; // here we remove the last char, so we could remove 2 occurencies at the same time } else { int x = occ_posit + N - 1; while (A.charat[x + 1] == A.charat[x]) x--; // find the first char different from the last one A.remove[x]; // B does not ends as it starts, so if there are overlapping instances they overlap by more than one char. Removing the first that is not equal to the char following B instance, we kill both occurrencies at once. } } </code></pre> <p>Let's explain with an example:</p> <p>A = "123456789000987654321" B = "890"</p> <p>read this as a table:</p> <pre><code>occ_posit: 123456789012345678901 A = "123456789000987654321" B = "890" </code></pre> <p>first occurrence is at occ_posit = 8. B does not end as it starts, so it get into the second loop:</p> <pre><code>int x = 8 + 3 - 1 = 10; while (A.charat[x + 1] == A.charat[x]) x--; // find the first char different from the last one A.remove[x]; </code></pre> <p>the while find that A.charat11 matches A.charat[10] (="0"), so x become 9 and then while exits as A.charat[10] does not match A.charat9. A then become:</p> <p>A = "12345678000987654321"</p> <p>with no more occurencies in it.</p> <p>Let's try with another: A = "abccccccccc" B = "abc"</p> <p>first occurrence is at occ_posit = 1. B does not end as it starts, so it get into the second loop:</p> <pre><code>int x = 1 + 3 - 1 = 3; while (A.charat[x + 1] == A.charat[x]) x--; // find the first char different from the last one A.remove[x]; </code></pre> <p>the while find that A.charat4 matches A.charat[3] (="c"), so x become 2 and then while exits as A.charat[3] does not match A.charat2. A then become:</p> <p>A = "accccccccc"</p> <p>let's try with overlapping:</p> <p>A = "abcdabcdabff" B = "abcdab"</p> <p>the algorithm results in: A = "abcdacdabff" that has no more occurencies.</p> <p>finally, one letter overlap:</p> <p>A = "abbabbabbabba" B = "abba"</p> <p>B end as it starts, so it enters the first if:</p> <pre><code>if (A.charat[occ_posit] == A.charat[occ_posit+1]) A.remove[occ_posit - 1]; // no reason to remove A[occ_posit] here else A.remove[occ_posit]; // here we remove the last char, so we could remove 2 occurencies at the same time </code></pre> <p>that lets the last "a" of B instance to be removed. So:</p> <p>1° step: A= "abbbbabbabba" 2° step: A= "abbbbabbbba" and we are done.</p> <p>Hope this helps</p> <p>EDIT: pls note that the algotirhm must be corrected a little not to give error when you are close to the A end with your search, but this is just an easy programming issue.</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.
    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