Thread: recursion problem

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    4

    recursion problem

    I am working on a project and I need some help on one part.

    How do you write a recursive function to rearrange strings (permutations) in all possible combinations.

    Like if the input was ABC:

    ABC
    ACB
    BAC
    BCA
    CAB
    CBA

    would you switch letters or is there an easier way...

    Thanks,

    Dustin

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Something based on this perhaps

    "A" + permute("BC")
    "B" + permute("AC")
    "C" + permute("AB")

    Basically, put each char in the left position in turn, and permute the rest.

    permute stops being recursive when you do permute("")
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 09:45 PM
  2. Recursion problem
    By trnd in forum C Programming
    Replies: 2
    Last Post: 02-01-2009, 03:06 PM
  3. Problem of understanding recursion
    By ixing in forum C Programming
    Replies: 2
    Last Post: 05-02-2004, 03:52 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. recursion problem
    By ender in forum C++ Programming
    Replies: 1
    Last Post: 11-11-2001, 02:25 PM