This page is still under construction.

Parts of this page are still being built. What you see may change.

Mutating DNA

Time limit1sMemory limit2048 MB

Summary
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 −1-1 if one sequence cannot be turned into the other using mutations.

Grace is analysing two DNA sequences aa and bb, both with nn elements indexed from 00 to n−1n - 1. Your task is to help Grace answer qq questions of the form: what is the mutation distance between the substring a[x..y]a[x..y] and the substring b[x..y]b[x..y]? Here a substring s[x..y]s[x..y] of a DNA sequence ss is a sequence of consecutive characters of ss whose indices run from xx to yy inclusive. In other words, s[x..y]s[x..y] is the sequence s[x]s[x+1]…s[y]s[x]s[x + 1] \dots s[y].

Constraints

  • 1≤n,q≤100 0001 \le n, q \le 100\,000
  • 0≤x≤y≤n−10 \le x \le y \le n - 1
  • Each character of aa and bb is one of "A", "T", and "C".

Examples1

  1. Example 1

    Input
    1 1
    A
    A
    0 0
    
    Expected output
    0