The Hamming distance between two equal-length sequences counts point mutations, which is the
simplest possible molecular clock: more substitutions implies more evolutionary time since
divergence. It underpins SNP calling and read-error estimation, and it's the base case that
edit-distance and alignment algorithms generalise.
Statement
Given two DNA strings of equal length, count the positions at which their symbols differ.
This is the Hamming distance.
The two strings arrive on two separate lines. Print a single integer on one line.
Input — read from standard input
| Variable | Type | Description |
|---|---|---|
s
line 1
|
str |
The first DNA string
1 <= len(s) <= 1000, uppercase A, C, G, T only
|
t
line 2
|
str |
The second DNA string
len(t) == len(s)
|
These variables are already read for you in the starter code on the right.
Output
int the number of positions at which s and t differ
Sample Cases
GAGCCTACTAACGGGAT
CATCGTAATGACGGCCT
7
ACGT
ACGT
0
Submit also runs your code against 4 hidden test cases. Hidden inputs are never shown — if one fails you'll get its number and a description of the mismatch, not the data.
Constraints
1 <= length(s) <= 1000length(t) == length(s)— the strings are always the same length- Uppercase
A,C,G,Tonly
Further Reading
sum(1 for a, b in zip(s, t) if a != b)in Python.- In R,
sum(strsplit(s, "")[[1]] != strsplit(t, "")[[1]]).
My Notes
Private to you. Saved to your account, separate from your code.
- Ctrl + Enter run · Ctrl + Shift + Enter submit