Note that there are some explanatory texts on larger screens.

plurals
  1. POFinding variants of a string (only deletion, no transposition)
    primarykey
    data
    text
    <p>Given a string, I want to find all variants without transposition, only deletion. For example, given the string:</p> <pre><code>helloo </code></pre> <p>The list of variants would be as follows (separated by white space).</p> <pre><code>helloo hello heloo helo </code></pre> <p>My solution so far is to move through each character, and then if the current character matches the next character, recursively try the original and the deleted character version, as follows.</p> <pre><code>// takes String with at most two consecutive characters of any character, // and returns an Iterable of all possible variants (e.g. hheello -&gt; heello, hhello, ...) private static Iterable&lt;String&gt; findAllVariants(String word) { StringBuilder variant = new StringBuilder(word); Queue&lt;String&gt; q = new LinkedList&lt;String&gt;(); findAllVariants(word, variant, 0, q); return q; } // helper method private static void findAllVariants(String word, StringBuilder variant, int currIndex, Queue&lt;String&gt; q) { if (currIndex == variant.length() - 1) q.add(variant.toString()); for (int i = currIndex; i &lt; variant.length() - 1; i++) { char thisChar = variant.charAt(i); char nextChar = variant.charAt(i+1); if (thisChar == nextChar) { // get all variants with repeat character findAllVariants(word, variant, i+1, q); // get all variants without repeat character; variant = variant.deleteCharAt(i); findAllVariants(word, variant, i, q); } } } </code></pre> <p>However, I end up getting a large number of copies of answers, and none of others. When I do my algorithm on paper, it seems correct. What am I doing wrong?</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. 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