Skip to main content
deleted 8 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

all All combinations of a string with special characters

However, I think this is a highly inefficient code as I need to do the same operation for all the words in the given Set of words(Set<String> words).

Is there any faster way to generate the same result??

  Any improvements in the current function to enhance speed and readability??

all combinations of a string with special characters

However, I think this is a highly inefficient code as I need to do the same operation for all the words in the given Set of words(Set<String> words)

Is there any faster way to generate the same result??

  Any improvements in the current function to enhance speed and readability??

All combinations of a string with special characters

However, I think this is a highly inefficient code as I need to do the same operation for all the words in the given Set of words(Set<String> words).

Is there any faster way to generate the same result? Any improvements in the current function to enhance speed and readability?

Source Link
Aneesh K
  • 333
  • 2
  • 5
  • 12

all combinations of a string with special characters

I have written a function which generates combinations of string by replacing 1 or more characters by the symbol "*".

The code is as follows:

/**
        this following code is responsible to generate all possible combiantions of letters from a 
        word with *
        for eg. word = "fin", 
        wordSet --> {fin, fi*, f**, f*n, **n, ***,*in}

*/
public static void generateCombinations(Set<String> words){
        
        Set<String> wordSet = new HashSet<>();


        for (String word : words){
            
            StringBuilder asterisk = new StringBuilder("*");

            while (asterisk.length() != word.length()){
                
                int startIndex = 0;
                while (startIndex + asterisk.length() < word.length()) {
                    String tempWord = word.substring(0, startIndex)
                            + asterisk.toString()
                            + word.substring(startIndex +  asterisk.length());
                    wordSet.add(tempWord);
                    startIndex ++;
                }
                wordSet.add(word.substring(0, Math.abs(startIndex - asterisk.length()+1 ))+ asterisk.toString() );
                
                //loop condition incrementor
                asterisk.append("*");
            }
            //all *'s
            wordSet.add(asterisk.toString());
            //no *
            wordSet.add(word);
        }
        //System.out.println(wordSet);
        
    }

However, I think this is a highly inefficient code as I need to do the same operation for all the words in the given Set of words(Set<String> words)

Is there any faster way to generate the same result??

Any improvements in the current function to enhance speed and readability??