DNA Mutations

Easy · rating 1000 · strings

Two DNA strands of equal length are compared. Read two lines, strands a and b. Print the number of positions at which the nucleotides differ (the Hamming distance).

Editorial

DNA Mutations counts the positions at which two equal-length strands differ — the Hamming distance, used throughout genetics and error-correcting codes.

Approach

Walk both strings in lockstep and count mismatches. Because the lengths are equal, a single pass suffices.

return sum(1 for x, y in zip(a, b) if x != y)

Complexity

Time: O(n). Space: O(1).

Open DNA Mutations in Code Arena →