8

I know this might be one of the silliest questions out there but I'm going ahead and ask it here since I've lost practice in mathematics.

I have been reading that the number of subsequences in a string is $2^x$ where $x$ is the length of the string and I have also worked some examples which agree with this, however I still am not very comfortable with this explanation. Can somebody formally prove this in simple terms please? More specifically where does the $2$ come from?

lvella
  • 358

1 Answers1

10

In constructing a subsequence, each element may be present or absent. That gives two choices for each. As there are $x$ choices, you multiply that many $2$'s together. It is the same as finding the number of subsets of a set, the order carries over from the original sequence to the subsequence. As an example, take the sequence $1,2,3$ and try to find all the subsequences by hand. You should find $2^3=8$ of them. Each corresponds to one of the binary numbers from $000$ to $111$, where a $0$ says that element is not present in the subsequence and a $1$ says it is.

Ross Millikan
  • 374,822
  • Nice! It was not clear for me that $1, 3$ is consider a subsequence. http://www.wikiwand.com/en/Subsequence – Eduardo Reis May 11 '18 at 19:28
  • 1
    @EduardoReis: yes, it is. Sometimes we speak of contiguous subsequences which are required not to have gaps in them, but a general subsequence can. – Ross Millikan May 11 '18 at 19:39
  • @RossMillikan So the total number of subsequences in a string is $2^n$, where $n$ is the length of the string. Wikipidea (https://en.wikipedia.org/wiki/Subsequence) seems to wrongly say that The list of all subsequences for the word "apple" would be "a", "ap", "al", "ae", "app", "apl", "ape", "ale", "appl", "appe", "aple", "apple", "p", "pp", "pl", "pe", "ppl", "ppe", "ple", "pple", "l", "le", "e", ""., because this would only be the list of the unique subsequences (24), while for the word apple, all the subsequences will be 32 (as p appears twice in the string). Correct? – user3019105 Dec 12 '19 at 10:38
  • 1
    @user3019105: $2^n$ assumes the elements of the sequence are distinct. In the Wikipedia case, the two $p$s in apple ruin that because if you just take one $p$ you can't tell if it is the first or the second. $pp$ has only three subsequences, not four. The other three letters multiply that by $8$ to get $24$. – Ross Millikan Dec 12 '19 at 14:53