Link to home
Start Free TrialLog in
Avatar of Ajay Sharma
Ajay SharmaFlag for India

asked on

Generate Permutation and Combinations of a given string

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.
SOLUTION
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ajay Sharma

ASKER

Thanks a lot Dhaest and Idle_Mind for your quick and valuable inputs.
I will revert after testing your suggested codes.

Thanks
Ajay Sharma
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.
Thanks