A synthetic biology lab is designing short RNA molecules over the alphabet {A, C, G, U}.
For this exercise, assume that the lab wants to discard every sequence that contains two consecutive guanines (GG), and wants to know how many candidate sequences of a given length are left.
For example, AGCG is allowed, while AGGC is not.
Problem Description
Given an integer n, compute the number of RNA sequences of length n over {A, C, G, U} that do not contain GG as a substring.
For example, if n = 2 there are 4 × 4 = 16 possible sequences and only one of them (GG) is forbidden:
AA AC AG AU
CA CC CG CU
GA GC GU
UA UC UG UU
→ 15 valid sequences.
Function Description
Implement the following function:
def countNoGG(n: int) -> int:
# Write your code hereParameters
n: length of the sequence (n ≥ 0)
Returns
int: number of sequences of lengthnthat do not containGG
Example
Input
2
Output
15
💡 Hint
Listing all the 4^n sequences is far too slow. Instead, look at the last nucleotide of a valid sequence of length n:
- it is
A,CorU(3 choices): the firstn-1nucleotides can be any valid sequence of lengthn-1; - it is
G: then the nucleotide before it must beA,CorU(3 choices), and the firstn-2nucleotides can be any valid sequence of lengthn-2.
with count(0) = 1 (the empty sequence) and count(1) = 4.
Then fill a 1D table, so that every value is computed only once:
| 0 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| 1 | 4 | 15 | 57 | ? | ? |
What is the time and space complexity of your solution? How does it compare with generating all the sequences?
Test
Input2
10
Output2
641520