Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

string combinatorics, generate  nCr strings from n strings

Posted on 2011-09-22
8
Medium Priority
?
578 Views
Last Modified: 2012-05-12
I have a set of n strings. I would like to extract all combinations of strings from these n strings . (all nCr combinations). How can  I do this programmatically.
For example :
Input : a,b,c,d,e
Output :
a,b,c,d
a,b,c,e
a,b,d,e
a,c,d,e
b,c,d,e
a,b,c
a,c,d
a,d,e
......and so on till

a,b
b,a
and so on till
a
b
c
d
e
Please help thanks
0
Comment
Question by:TrialUser
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 36580129
The best way is to make a function that finds all string combinations of length N and use that recursively.
Then you just call it for each length.

  
ArrayList FindAllStrings(string input)
{
  ArrayList strings;

  for(int i = 0; i<=input.Length(); i++)
  AddStrings(strings, input, i, "");
}

AddStrings(ArrayList strings, string input, int N, string leading = "")
{
  if(N == 0)
    strings.Add(leading);
  else
  {
    for(int i = 0; i + N < input.Length(); i++)
      addStrings(strings, input.SubString(i+1), N-1, leading + input[i]);
  }

Open in new window

0
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 2000 total points
ID: 36580206
Tested and fixed
        private ArrayList FindAllStrings(string input)
        {
            ArrayList strings = new ArrayList();

            for (int i = 0; i <= input.Length; i++)
                AddStrings(strings, input, i, "");

            return strings;
        }

        private void AddStrings(ArrayList strings, string input, int N, string leading = "")
        {
            if (N == 0)
                strings.Add(leading);
            else
            {
                for (int i = 0; i + N <= input.Length; i++)
                    AddStrings(strings, input.Substring(i + 1), N - 1, leading + input[i]);
            }
        }

Open in new window

0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 36580224
If you want the commas, change line 18 to
AddStrings(strings, input.Substring(i + 1), N - 1, leading + (leading.Length > 0 ? ",":"") + input[ i]);
0
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 36581969
Use the libaray here written by Adrian Akison:
http://www.codeproject.com/KB/recipes/Combinatorics.aspx

Simple Demo: Idle-Mind-502573.flv

Click on Project --> Add Reference --> Browse --> Facet.Combinatorics.dll

Then you want to use the Variations class like this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.DataSource = Variations(textBox1.Text);
        }

        private List<string> Variations(string input)
        {
            List<string> values = new List<string>();
            for (int i=0; i < input.Length; i++)
            {
                values.Add(input.Substring(i,1));
            }

            List<string> results = new List<string>();
            for (int i = input.Length; i >= 1; i--)
            {
                Facet.Combinatorics.Variations<string> vars = new Facet.Combinatorics.Variations<string>(values.AsReadOnly(), i);
                foreach (IList<string> set in vars)
                {
                    results.Add(String.Join(",", set));
                }
            }
            return results;
        }
    }
}

Open in new window


Here's the output:
a,b,c,d
a,b,d,c
a,c,b,d
a,d,b,c
a,c,d,b
a,d,c,b
b,a,c,d
b,a,d,c
c,a,b,d
d,a,b,c
c,a,d,b
d,a,c,b
b,c,a,d
b,d,a,c
c,b,a,d
d,b,a,c
c,d,a,b
d,c,a,b
b,c,d,a
b,d,c,a
c,b,d,a
d,b,c,a
c,d,b,a
d,c,b,a
a,b,c
a,b,d
a,c,b
a,d,b
a,c,d
a,d,c
b,a,c
b,a,d
c,a,b
d,a,b
c,a,d
d,a,c
b,c,a
b,d,a
c,b,a
d,b,a
c,d,a
d,c,a
b,c,d
b,d,c
c,b,d
d,b,c
c,d,b
d,c,b
a,b
a,c
a,d
b,a
c,a
d,a
b,c
b,d
c,b
d,b
c,d
d,c
a
b
c
d

Open in new window

 
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 36582051
Idle_Mind, that does all the possible permutations of all the possible combinations. It's also using another library and even then, the code looks just about as long as mine.

TrialUser, I assume your inclusion of both a,b and b,a was accidental and you just wanted the combinations. To do all permutations in mine you would just change
input.Substring(i + 1)
to
input.Substring(0,i) + input.Substring(i + 1)
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 36582067
You do need to make sure to have
using System.Collections
at the top to use the ArrayList or you could modify the code to use whatever collection type you prefer.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 36582072
The library can also do Permuations and Combinations as well...it's pretty flexible.
0
 
LVL 13

Expert Comment

by:Ryan
ID: 36904484
Just last week I was solving a Permutation problem on ProjectEuler.net and came across this website. http://www.bearcave.com/random_hacks/permute.html

In particular, I found this graphic interesting, http://www.bearcave.com/random_hacks/permute_diagram.jpg

Merely implementing that, with an otter loop that iterates through removing letters solves it.
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

721 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