6

Given a string of length 'n'. How do I get all the sub sequences of length r(r<=n). I was thinking of doing it using dynamic programming but could not come up with a good solution. I want a pseudo code for this.

Eg. given the string "abc" and r = 2.

OUTPUT : ab ba ac ca bc cb

thanks in advance

Keen Sage
  • 1,799
  • 4
  • 21
  • 39

3 Answers3

3

It is important to see the difference between all possible substrings (contiguous sequence) and generally subsequences (not necessarily contiguous).

If this is true, then what you were asked is called combinations, and it is nice first to estimate how many of them you have given the length of your string and the size of your subsequence.

A recursive algorithm is the best approach here: it allows you to have a the length of your subsequence as a variable. You will find a perfect answer in another thread here.

Community
  • 1
  • 1
Alexander Galkin
  • 10,800
  • 9
  • 56
  • 111
  • I wouldn't say that a recursive algorithm is the "best" approach; it's perhaps the most obvious. If the strings are very long, then a recursive approach will lead to a stack overflow. – Oliver Charlesworth Dec 11 '11 at 19:55
  • 2
    @OliCharlesworth This is the common drawback of every recursive algorithm, not specifically of this one, as confirmed by the name of this site, by the way :-) – Alexander Galkin Dec 11 '11 at 20:01
  • IMO a recursive algorithm is a good starting point to find a solution to a problem. After finding the recursive solution it always can be transformed into a iterative solution (if this is for any reason necessary). – Christian Ammer Dec 11 '11 at 20:08
  • @ChristianAmmer From my experience of job interviews they often ask the questions that imply first deriving a recursive algorithm with subsequent discussion on its pros and contras and, on request, its transformation to an iterative one. – Alexander Galkin Dec 11 '11 at 20:11
1

Try the following code(no pseudo code but understandable, I hope):

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

string S;
int n;
vector<string> L;


void F(int index, int length, string str)
{
  if (length == 0) {
    L.push_back(str);
  } else {
    for (int i = index; i < n; i++) {
      string temp = str;
      temp += S[i];
      F(i + 1, length - 1, temp);
    }
  }
}

int main()
{
  S = "abcde"; // replace with your string
  n = S.length();
  int k = 3; // or what you want
  F(0, k, string(""));

  int count = 0;

  for (int i = 0; i < int(L.size()); i++) {
    string temp = L[i];
    sort(temp.begin(), temp.end());
    do {
      cout << temp << endl;
      count++;
    } while (next_permutation(temp.begin(), temp.end()));
  }


  cout << endl << "count = " << count << endl;
  return 0;
}
rendon
  • 2,293
  • 1
  • 17
  • 23
0
    public static void combinations(String suffix,String prefix){
            if(prefix.length()<0)
                      return;
            System.out.println(suffix);
            for(int i=0;i<prefix.length();i++) {
                 combinations(suffix+prefix.charAt(i),prefix.substring(i+1,prefix.length()));
        }
    }


//call above function like: combinations("","abcd");