Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Data Structure -- Permutations
varalu
post Dec 11 2007, 06:59 AM
Post #1


Super Member
*********

Group: [HOSTED]
Posts: 284
Joined: 1-October 07
From: India
Member No.: 50,968



Write an algorithm to print all the permutations of a string.

Input: man
Output: man
mna
amn
anm
nam
nma

Give solutions optimized for Speed. Optimize for Size too.



What is a permutation?
QUOTE
Permutation is the rearrangement of objects or symbols into distinguishable sequences. Each unique ordering is called a permutation. For example, with the numerals one to six, each possible ordering consists of a complete list of the numerals, without repetitions. There are 720 total permutations of these numerals, one of which is: "4, 5, 6, 1, 2, 3".

The general concept of permutation can be defined more formally in different contexts:

* In set theory, a permutation is an ordered sequence containing each symbol from a set once, and only once. A permutation is distinct from a set or combination, in that the ordering of elements in a set is not considered relevant for sets or combinations. In other words, the set-theoretic definition of permutation is that of a one-to-one correspondence, or bijection, of labeled elements with "positions" or "places" which are arranged in a straight line.
* In abstract algebra and related areas, the elements of permutation may not be arranged in a linear order, or indeed in any order at all. Under this refined definition, a permutation is a bijection from a finite set, X, onto itself. This allows for the definition of groups of permutations; see permutation group.
* In combinatorics, the term permutation also has a traditional meaning which includes ordered lists without repetition and where one or more elements from the list are omitted from the distinguishable orderings; for example, a permutation of "1,2,4,3" with "5" and "6" omitted.



Here i have one possible solution from my friend.... (Pallavi)
1.Concat the input string with itself : manman
2. initialise the ptr to first element
3. Print it length of the string
4. Move the ptr by one position ( keep incrementing till length times) and perform step 3
5. reverse the whole string and repeat steps 2, 3, 4

Any better solutions? WELCOME !!!
Go to the top of the page
 
+Quote Post
laexter
post Dec 12 2007, 04:00 PM
Post #2


Newbie [Level 1]
*

Group: Members
Posts: 18
Joined: 3-November 07
From: Jakarta, Indonesia
Member No.: 52,426



CODE
$tmp = array();
$s = 'the string you wish to permute, in the example, e.g. man';
$out = '';
$ctr=0;

function perm($ctr) {
    global $out;
    global $s;
    global $tmp;
    //echo $out;
    if ($ctr == strlen($s)) {
        echo $out . "\n";
    }
    else {
        for ($i=0;$i<strlen($s);$i++) {
            //echo 'a';
            if (!$tmp[$i]) {
                $tmp[$i]=TRUE;
                $out .= $s[$i];
                perm($ctr+1);
                $tmp[$i]=FALSE;
                $out = substr($out, 0, -1);
            }
        }
    }
}

perm(0);


is something I have in my computer. (it is PHP if you haven't noticed)
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Data Structure Questions(4)
  2. Data Structures -- Binary Tree -- Mirror Image(0)
  3. Data Structures -- Linked List(7)
  4. Data Structures -- Binary Tree -- Structurally Same(4)
  5. Data Structures -- Linked List -- Reverse(3)
  6. Data Structures -- String -- Palindrome(4)
  7. Data Structures -- Linked List -- Point Of Merging(2)
  8. Data Structures -- String -- Arrange Based On Repetition(1)
  9. Data Structure -- Arrays -- Odd Number Of Elements(0)
  10. Data Structures -- Expression Trees(0)
  11. Data Structure -- Trees -- Threaded Binary Tree(0)
  12. Data Structure -- Queue -- Implement Using Stack(0)


 



- Lo-Fi Version Time is now: 26th July 2008 - 07:14 AM