Basically what i want to do is this:
Input Example 1:
abc
Output:
abc
acb
bac
bca
cba
cab
Input Example 2:
abcd
Output:
abcd
abdc
acdb
acbd
adbc
adcb
bacd
badc
bcda
bcad
bdac
bdca
cabd
cadb
cbda
cbad
cdab
cdba
dacb
dabc
dbca
dbac
dcda
dcad
What i want to do is to print all the different possibilities of a given string.
The current method i'm following:
*Swap the first character with the second character
*Swap the second character with the third character
*Repeat swapping until at least one of the character involved in swapping is the null character.
The problem with my current method:
*Hell lot of duplications,i.e,"input" outputting atleast more than once.
*Doesn't output all the combinations.
What i'd like for you to do is give me some hints as to what i'm doing wrong and give me a working pseudo code to solve my crisis(not a direct solution,please).
Yes,i could've posted this in some programmer forum,but i'm not sure how i'm supposed to form my question.I'd go there and indefinitely make myself look a fool This forum is more safe since we've already established i'm a moron.
P.S.
I spend all my last years vacation trying to find a solution to this problem and i did.The problem is,it was kind of like a fluke,i think.
I spend so much time on it,it worked at last,i slept and i woke up,i tried to read my code and i was like wtf is this.
Short story short: I solved the problem but after a good night's sleep i no longer remembered how i solved it,and i couldn't even figure what i did in my code either,yeah my code was that ugly.
I've tested that program,and it works,but although it works i'm not sure if its the most efficient,also there are a lot of duplications,so instead of my cave man problem solving skills,i was wondering if someone could provide me with a methodical approach to the problem.
If its possible i'd like a Recursive as well as an iterative approach to solve the problem(i prefer iterative solution though).
Input Example 1:
abc
Output:
abc
acb
bac
bca
cba
cab
Input Example 2:
abcd
Output:
abcd
abdc
acdb
acbd
adbc
adcb
bacd
badc
bcda
bcad
bdac
bdca
cabd
cadb
cbda
cbad
cdab
cdba
dacb
dabc
dbca
dbac
dcda
dcad
What i want to do is to print all the different possibilities of a given string.
The current method i'm following:
*Swap the first character with the second character
*Swap the second character with the third character
*Repeat swapping until at least one of the character involved in swapping is the null character.
The problem with my current method:
*Hell lot of duplications,i.e,"input" outputting atleast more than once.
*Doesn't output all the combinations.
What i'd like for you to do is give me some hints as to what i'm doing wrong and give me a working pseudo code to solve my crisis(not a direct solution,please).
Yes,i could've posted this in some programmer forum,but i'm not sure how i'm supposed to form my question.I'd go there and indefinitely make myself look a fool This forum is more safe since we've already established i'm a moron.
P.S.
I spend all my last years vacation trying to find a solution to this problem and i did.The problem is,it was kind of like a fluke,i think.
I spend so much time on it,it worked at last,i slept and i woke up,i tried to read my code and i was like wtf is this.
Short story short: I solved the problem but after a good night's sleep i no longer remembered how i solved it,and i couldn't even figure what i did in my code either,yeah my code was that ugly.
I've tested that program,and it works,but although it works i'm not sure if its the most efficient,also there are a lot of duplications,so instead of my cave man problem solving skills,i was wondering if someone could provide me with a methodical approach to the problem.
If its possible i'd like a Recursive as well as an iterative approach to solve the problem(i prefer iterative solution though).