Evolution as a Sequence of Mistakes

A mutation is simply a mistake that occurs during the creation or copying of a nucleic acid, in particular DNA. Because nucleic acids are vital to cellular functions, mutations tend to cause a ripple effect throughout the cell. Although mutations are technically mistakes, a very rare mutation may equip the cell with a beneficial attribute. In fact, the macro effects of evolution are attributable by the accumulated result of beneficial microscopic mutations over many generations.

The simplest and most common type of nucleic acid mutation is a point mutation, which replaces one base with another at a single nucleotide. In the case of DNA, a point mutation must change the complementary base accordingly; see Figure 1.
Two DNA strands taken from different organism or species genomes are homologous if they share a recent ancestor; thus, counting the number of bases at which homologous strands differ provides us with the minimum number of point mutations that could have occurred on the evolutionary path between the two strands.
We are interested in minimizing the number of (point) mutations separating two species because of the biological principle of parsimony, which demands that evolutionary histories should be as simply explained as possible.
Counting Point Mutations(汉明距离) - 图1
Figure 1. A point mutation in DNA changing a C-G pair to an A-T pair.

Problem

Given two strings ss and tt of equal length, the Hamming distance between s and t, denoted dH(s,t), is the number of corresponding symbols that differ in ss and t. See Figure 2.
Given: Two DNA strings s and t of equal length (not exceeding 1 kbp).
Return: The Hamming distance dH(s,t).
Counting Point Mutations(汉明距离) - 图2
Figure 2. The Hamming distance between these two strings is 7. Mismatched symbols are colored red.

Sample Dataset

  1. GAGCCTACTAACGGGAT
  2. CATCGTAATGACGGCCT

Sample Output

  1. 7

Solution

其实本题很简单,就是汉明距离,如果两个字符不等,那就 +1 ,因为题目说的是等长,因此非常容易。

  1. class Solution:
  2. def HammingDistance(self, seq1: str, seq2: str) -> int:
  3. return sum(base1 != base2 for base1, base2 in zip(seq1, seq2))
  4. seq1 = "GAGCCTACTAACGGGAT"
  5. seq2 = "CATCGTAATGACGGCCT"
  6. print(Solution().HammingDistance(seq1, seq2)) # 7