The closest strains
- O(n²) · Very hard
- Full plan
- Python
- JavaScript
- strings
- lists
- loops
Problem
Diego has just sequenced several strains of the same virus. If two of them barely differ, they may come from the same outbreak, so he wants to know how close the two most similar ones are.
Each strain is a string of A, C, G and T, and they all have the same length. The distance between two strains is how many positions hold a different letter: "ACGT" and "AGGA" differ at 1 and at 3, so they are 2 apart.
Write a function that takes the list of strains (at least two) and returns, as a whole number, the smallest distance between two strains in different places in the list. A strain is never compared with itself, but if two places hold the same string, their distance is 0.
With ["ACGT", "TCGA", "TGGA"]: the first and second are 2 apart, the first and third 3, and the second and third 1. You return 1.
Examples
The example
["ACGT", "TCGA", "TGGA"] → 1
Just two, completely different
["AAAA", "TTTT"] → 4
The close ones are not neighbors
["CGTA", "GCAT", "CGAA", "TTTT"] → 1
The smallest is 2
["AACC", "GGTT", "AATT", "CCGG"] → 2
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def closest_pair(strains):
passJavaScript
function closestPair(strains) {
}It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.