Training Site
Sideways banner 8b44c02b768aa359c4d0a2bce3b247c6d7ab888f8b26852e9bfc3659c8c25612

Anagrams

Input: Standard Input (stdin)
Output: Standard Output (stdout)
Memory limit: 64 megabytes
Time limit: 2.0 seconds

Anagrams

The anagrams of a word are all the possible permutations of the letters in that word.

Write a program which is given a word and which outputs, one per line, all the anagrams of that word.

Use this recursive algorithm:

printAnagram (prefix, word)
    If word is length 1 and not the starting word then
        print the prefix and the word
    Otherwise
    For each letter in the word
        newPrefix = Prefix + letter
        rest = Word without letter
        printAnagram(newPrefix, rest)
  • Sample Input 1

    cat
    

    Sample Output 1

    cta
    act
    atc
    tca
    tac