Anagram

You are given a word. Generate all the anagrams of that word.

Anagrams of a word means all possible permutations of that word.

Example:

The word "ANT" has following anagrams- There are total 6 anagrams for the word "ANT".

View Code

Loop through the word, take one letter and rearrange other letters.

For the word "ANT", it will be like below -

i=0, letter=A, rearrange "NT" i=0, letter=A, rearrange "TN" i=1, letter=N, rearrange "AT" i=1, letter=N, rearrange "TA" i=2, letter=T, rearrange "AN" i=2, letter=T, rearrange "NA"

Lets simplify the problem. The anagram of 1 letter is the letter only.

Anagram of "A" = "A"

For two letter word, we loop through the word. Each time we separate one letter and anagram the rest of the letters.

...
Image: Anagram of two letter word

For three letter word, it will be like below-

...
Image: Anagram of three letter word

How to solve this problem?
We can solve this problem using recursive function.

big-O Analysis
It takes O({{write}}) time