permutations

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chandru50
    New Member
    • Dec 2006
    • 5

    permutations

    pl. help.

    write a Cprogramme to find all permutations ( a string of given characters)
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    I forget which is combinations and which is permutations but I think (and correct me if I'm wrong), that permutations are all the different ways we can create a string of n characters from a string of m (>n) characters. The difficulty is if the characters are not unique (because CC and CC are indistinguishab le) - Assuming, then (because it's easier) that you have a string of unique characters the method would be something like (There may be some bugs uin my thinking, but at least this should give some idea):

    Code:
    {
      Get String from user.
      Get permutation length from user:
      Check perm length > String length;
      startPermutations("",input, length);
    }
    
    startPermutations(String, soFar, String input, int length)
    {
      if (length=0)
      {
        print soFar;
      }
      else
      {
        for(number of  character in the String)
        {
          soFar=soFar+currentChar;
          input=input.subString(1, input.length);
          startPermutations(soFar, input, length -1);
          move currentChar to back of String, so we can repeat with the next;
        }
      }
    }

    Comment

    Working...