Solved

Generate Permutation and Combinations of a given string

Posted on 2012-03-16
6
406 Views
Last Modified: 2012-03-26
Hi,

I have a String (e.g. GUIDE ANTIMICROBIAL TARGET) which contains some words ( one or more).

I want to generate all Permutations and Combinations with these words and the output should  be like following:

Level1
GUIDE
ANTIMICROBIAL
TARGET

Level2
GUIDE ANTIMICROBIAL
ANTIMICROBIAL GUIDE
GUIDE TARGET
TARGET GUIDE
TARGET ANTIMICROBIAL
ANTIMICROBIAL TARGET

Level3
GUIDE ANTIMICROBIAL TARGET
......... of all 3 words ...........


kindly suggest some code which will generate strings for all levels (1,2,3) and only provide the C# codes.
0
Comment
Question by:Ajay Sharma
  • 3
  • 2
6 Comments
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 250 total points
ID: 37729204
See following example.
I perform it in 2 steps: first create a list of all combinations, and use these to create the permutations
EE.txt
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 37729264
Attached an improved version where you can pass more than 3 words (and it will work).

Also possible to tell which permutation level you want


Testing is included see
        private void Form1_Load(object sender, EventArgs e)
        {
            List<string> combination = CombinateWords(new[] { "bat","cow","dog","mouse" },null);

            List<string> permutations = new List<string>();
           
            foreach (string combinatedString in combination)
            {
                permutations.AddRange(PermuteWords(combinatedString));

            }

            List<string> permutLevel = CombinationPermutation(new[] {"bat", "cow", "dog", "mouse"}, 2);
        }
EE.txt
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 37729698
Take a look at my answer here:
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_27321920.html#a36581969

and change lines 27 thru 30 from:

            for (int i=0; i < input.Length; i++)
            {
                values.Add(input.Substring(i,1));
            }

to:

            values.AddRange(input.Split(" ".ToCharArray()))
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 18

Author Comment

by:Ajay Sharma
ID: 37729900
Thanks a lot Dhaest and Idle_Mind for your quick and valuable inputs.
I will revert after testing your suggested codes.

Thanks
Ajay Sharma
0
 
LVL 18

Author Comment

by:Ajay Sharma
ID: 37764581
I didn't got time to apply your codes but one of my Google search gave me this famous PermuteUtils link. It worked great for me after modifying a bit.

Thanks all for your time, efforts and help.
0
 
LVL 18

Author Closing Comment

by:Ajay Sharma
ID: 37764586
Thanks
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now