java - Finding variants of a string (only deletion, no transposition) -


given string, want find variants without transposition, deletion. example, given string:

helloo 

the list of variants follows (separated white space).

helloo hello heloo helo 

my solution far move through each character, , if current character matches next character, recursively try original , deleted character version, follows.

// takes string @ 2 consecutive characters of character,  // , returns iterable of possible variants (e.g. hheello -> heello, hhello, ...) private static iterable<string> findallvariants(string word) {     stringbuilder variant = new stringbuilder(word);     queue<string> q = new linkedlist<string>();     findallvariants(word, variant, 0, q);     return q; }  // helper method private static void findallvariants(string word, stringbuilder variant, int currindex, queue<string> q) {     if (currindex == variant.length() - 1) q.add(variant.tostring());      (int = currindex; < variant.length() - 1; i++) {         char thischar = variant.charat(i);         char nextchar = variant.charat(i+1);         if (thischar == nextchar) {             // variants repeat character             findallvariants(word, variant, i+1, q);              // variants without repeat character;             variant = variant.deletecharat(i);             findallvariants(word, variant, i, q);         }     } } 

however, end getting large number of copies of answers, , none of others. when algorithm on paper, seems correct. doing wrong?

something along lines of following code enable possibilities (remember add word if needed). idea retreive possibilities removing 1 char (e.g. hello results in ello hllo helo hell). these results can in turn used possibilities removing 2 chars (remove 1 char again). resulting in llo elo ell ello , on...

list<string> getpossibilities(string word) {     int removechars = word.length() - 1;     list<string> possibilities = new arraylist();     list<string> options = arrays.aslist(word);     for(int = 0; <= removechars; i++) {         list<string> results = new arraylist();         for(string option : options) {           for(string result : removeonechar(option)) {                 if(!results.contains(result)) {                     results.add(result);                 }             }         }         possibilities.addall(results);         options = results;     }     return possibilities; }  private static list<string> removeonechar(string word) {     list<string> results = new arraylist();     for(int = 0; < word.length(); i++) {         int secondpart = + 2;         if(secondpart <= word.length()) {             results.add(                     word.substring(0, i)                      + word.substring(i + 1, word.length()));         }         else {             results.add(                     word.substring(0, i));         }     }     return results; } 

notice if(!contains(result)) in order prevent duplicates.

note i've used substring() accomplish this, you're approach removecharat() option. run tests see performs better decide 1 use. notice using latter possibly remove need of if in private method.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -