2

I would like to create all possible words(valid and invalid both) from the letters inputted from the user. For example, the letters are "a,t,e". The answer should be "a,t,e,at,ta,ae,ea,te,et,ate,aet,tea,tae,eta,eat"(No duplicates). I need to do this using javascript or jquery.

I have succeeded in creating all words of length the same as the number of letters.

        let genAnagrams= (word, anagram='',anagrams=[])=>{
            if(!word){
                anagrams.push(anagram);
                return;
            }
            for(var i=0;i<word.length;i++){
                anagram+= word[i];
                genAnagrams(word.slice(0,i)+ word.slice(i+1),anagram,anagrams);
                anagram=anagram.slice(0, anagram.length-1);
            }
            return anagrams;
        };

For input as "a,t,e", expected output would be a,t,e,at,ta,ae,ea,te,et,ate,aet,tea,tae,eta,eat"(No duplicates)

  • 3
    Possible duplicate of [get all combinations for a string](https://stackoverflow.com/questions/12048621/get-all-combinations-for-a-string) - See [this answer](https://stackoverflow.com/a/12048962/6624953) – frobinsonj Jun 20 '19 at 10:59
  • @frobinsonj I **think** that question does not expect the same output as what the OP wants. – Rishav Jun 20 '19 at 11:08
  • @Rishav on the contrary, it answers it exactly. https://jsfiddle.net/mku4hcLz/ – Rory McCrossan Jun 20 '19 at 11:20
  • @RoryMcCrossan You are absolutely right. But now looking at that answer I must say the OP there didn't ask for that. :P – Rishav Jun 20 '19 at 11:33

0 Answers0