1. Subsequences of Strings #

Created Friday 03 January 2020

Jargon:

(Note: Repetition is not allowed in both, wysiwyg)

How many for a string of length n:

e.g subarrays or substrings for for abc: “”, a, b, c, ab, bc, abc. 6+1

2.P and C: For every letter, we have a choice (take/leave). All are independent, product principle. And every set of choice is different from the other.

  1. So 2^n ^(including the empty string i.e take none).

e.g subsequences for abc: “”, a, b, c, ab, ac, bc, abc. 2^3^=8

Q) Print/Collect all subsequences for the given array. A) All Subsequences Input: String and output array address(of sufficient size). Output: void? But we will need to give length of the string array, as there’s no delimiter for the given array. Better if we return the size. So int should be the return type.

Base case: if(input.empty()) output[0] = “”; return 1; why? Because this the first element that will be printed. **Doubt(will “” come only once): **This will be executed only once. As we have only one recursion call, so only one anchor is possible.

We assume that our recursion will fill the output array with all subsequneces of substr(1). Here, we will prepend the elements with the current Activation record’s character, and add these to the ouput array. We will use count as a variable to make a for loop copy “these” after the latest vacant position, which is nothing but “count” output[count+i] = s[0]+output[i]; // we can append a character to a string it’s okay. This is a very clever trick. It’s ubiquitous. return 2*count; // Just return the current output array size, 2 times of what was there before the loop.

**Why? In ‘storage’, if we do calc first, we’ll have to pass the number of copies as we go, which is very painful. Better we do **CBA **and return1 for the empty array. This will ensure that we are right. And we don’t have to explicitly deal with the number of values. ** Remember that we need to return the number of elements, and creating another formal parameter will be of significant overhead.

Q) Code all possibilities of a keypad, single press mode. e.g 23 : dg dh di eg eh ei fg fh fi A) Using the same logic as recursion, do this. Let us make the base case sober(key here is returning 1 and “” when nothing is printed and with 1). Let’s code the printing of values in the calc step. Keypad print