Perfect Matchings and RNA Secondary Structures
This problem asks:
Given: An RNA string s of length at most 80 bp having the same number of occurrences of ‘A’ as ‘U’ and the same number of occurrences of ‘C’ as ‘G’.
Return: The total possible number of perfect matchings of basepair edges in the bonding graph of s.
Required reading
- RNA folding
- Nucleic acid secondary structure
- Zebrafish Yes, zebrafish.
- Stem loops
- Matching graphs
- Perfect matching
- Complete graphs
- Bonding graph
- Adjacency edges
- Recurrence relations
- Complete bipartite graphs
Restate the problem
I’m going to get an RNA string up to 80 characters long with the same number of A’s as U’s and the same number of C’s as G’s. I need to return the count of perfect matchings possible.
Solution steps
First, I wrote the recurrence relation equation that appears in the challenge explanation:
return ((2*n)-1) * x(n - 1)
Then, I tried to get the correct sample output given the sample dataset, but failed.
I realized that there are two disconnected graphs at work here. The A-U graph and the C-G graph. The total number of pairings is the number of A-U pairings times the number of C-G pairings.
The number of A-U graphs is A! The number of C-G graphs is C!
print(fact(A) * fact(G))
I used my simple recursive factorial code from an earlier challenge:
def fact(n):
if n < 2:
return 1
else:
return n * fact(n - 1)
I returned a correct response on my first attempt. 3,972 people solved this challenge before me.