• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 784
  • Last Modified:

Algorithm for Sequence Generator from given String

Hello,
Please help me writing a C++, Java program to generate sequences from the given string. For example from string str("ABC") we need to generate all possible sequences like "A B C AB AC BC ABC")

Thanks for the help
0
sriram
Asked:
sriram
1 Solution
 
ozoCommented:
Do you need the sequences to be generated in that order?
Can we assume that the string will be no longer than, say, 32 characters?
0
 
TommySzalapskiCommented:
A recursive solution would probably be the easiest.

string source = "ABC";
int length = source.length();
string current = "";

Generate(source, length, 0, 0,  current,);

Open in new window



Then
//start is where in the source to start, position is where in current we are modifying
void Generate(const string &source, const int length, int start, int position, string &current)
{
  if(start == length + 1)
    printf("%s\n", current); //or however you do output, just output current
  else
  {
    for(int i = start; i <= length; ++i) //Note: we allow i == length for the \0 to allow A, AB, etc
    {
      current[position] = source[i];
      Generate(source, length, i+1, position+1, current);
    }
  }
}

Open in new window

Note: This is C++ syntax and assumes order must be maintained. Also, I didn't test it. I mean, I'm not lazy. Any errors are left as an exercise to the student. Yeah.
0
 
sriramAuthor Commented:
Thanks for the suggestions. I had to modify the program to make it work.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now