Solved

Generate Permutation and Combinations of a given string

Posted on 2012-03-16
6
402 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

760 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