Mutating DNA
Time limit1sMemory limit2048 MB
Given two DNA strings over A, T, C, answer queries asking the minimum number of swaps to turn one substring into the other, or -1 if impossible.
- Level
Medium6 of 10
- Topics
- Prefix sum, String, Math, Combinatorics
- Solved
- No attempts yet
Problem
Grace is a biologist working at a bioinformatics firm in Singapore. As part of her job, she analyses the DNA sequences of various organisms. A DNA sequence is a string consisting of the characters "A", "T", and "C". In this task, DNA sequences do not contain the character "G".
A mutation is an operation on a DNA sequence that swaps two elements of the sequence. For example, a single mutation can turn "ACTA" into "AATC" by swapping the highlighted characters "A" and "C".
The mutation distance between two sequences is the minimum number of mutations needed to turn one sequence into the other, or if one sequence cannot be turned into the other using mutations.
Grace is analysing two DNA sequences and , both with elements indexed from to . Your task is to help Grace answer questions of the form: what is the mutation distance between the substring and the substring ? Here a substring of a DNA sequence is a sequence of consecutive characters of whose indices run from to inclusive. In other words, is the sequence .
Constraints
- Each character of and is one of "
A", "T", and "C".