0

Possible Duplicate:
Generate all unique substrings for given string

Can anyone show me some light on how to solve this problem?

I have a string with some values like "{1,2,3}" each number separated by a comma Now with that string i need to generate a new string ... the new string should look like "{},{1},{2},{3},{1,2},{1,3},{2,3},{1,2,3}"

it’s pretty easy to do it on your head but i can’t figure out how to make it work with an algorithm Can anyone explain what i should do?

i don’t want codes, just an explanation on how to do it please. I'm thinking recursive... but i cant think of a way

7
  • 2
    I think you need to explain what pattern you are trying to generate? Looks like all unique sorted substrings of the input Commented Sep 6, 2011 at 19:55
  • 1
    should {2,3} be in the new string? is this essentially generating all subsets from a set? ps. is this homework? Commented Sep 6, 2011 at 19:56
  • Are you looking for the logic that outputs every permutation of items in the list while retaining the order of the items? If so, did you accidentally omit {2,3}? Commented Sep 6, 2011 at 19:57
  • 1
    are you trying to the make power set? Commented Sep 6, 2011 at 19:58
  • @PengOne Not quite the same. If he wants {1,3} that is a non-sequential subset which isn't included in the logic of the other question, although the logic will still be very similar. Commented Sep 6, 2011 at 19:58

2 Answers 2

2

First of all, you need to iterate over the integers 0 to N, where N is the number of values in your string (let's call this iteration variable i). For each i you will then generate all sets containing i out of N numbers in your input.

For example, i == 0 will generate {}, i == 1 will generate {1},{2},{3} etc.

Now let's suppose that the value of i is fixed; this will let us proceed to understand what to do for each of these iterations. Obviously we will need some kind of nested loop, since we will be generating multiple (in the general case) output values for each value of i (e.g. we 'll generate 3 sets when i == 1, as shown earlier).

This is called generating combinations of i items out of N, and there is plenty of documentation available. For example, see Algorithm to return all combinations of k elements from n.

Sign up to request clarification or add additional context in comments.

Comments

0

For an alternative approach treat the question as a problem in binary. Your set has three elements, so you need three binary digits. Each member of the set can either be present (1) or absent (0) in the result. Hence you can map the required results onto three digit binary numbers:

000 -> {}
001 -> {3}
010 -> {2}
011 -> {2, 3}
100 -> {1}
101 -> {1, 3}
110 -> {1, 2}
111 -> {1, 2, 3}

It is not difficult to generate the numbers from 0 (000) to 7 (111) and to extract the required combinations from them.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.