Tous les défis
Moyen 0 résolus 0 % de réussite

DNA Short Tandem Repeats

For each short tandem repeat, find the longest run of back-to-back copies in a DNA sequence.

Ce que vous allez apprendre

  • Compare a substring of a string with a pattern
  • Count back-to-back copies starting at a position
  • Keep the best count for each pattern

Problème

Afficher la traduction

Forensic labs compare people by short tandem repeats (STRs): short DNA units such as AGAT that appear many times in a row. For each unit, the lab records the longest run of consecutive copies anywhere in the sequence. In TTAGATAGATAGATCAGATAGAT the unit AGAT appears three times in a row near the start and twice later, so its count is 3.

Implement dna_str_count(sequence, repeats).

Input

sequence: The DNA sequence, using the letters A, C, G and T.

repeats: The STR units to measure.

Output

Return one count per unit, in the same order as repeats: the largest number of copies that appear back to back. A unit that never appears has count 0.

Contraintes

0 ≤ length of sequence ≤ 5,000

1 ≤ number of units ≤ 10

Each unit has 1 to 8 letters

Exemples

Exemple 1
Entrée :
sequence = "TTAGATAGATAGATCAGATAGAT"
repeats = ["AGAT", "CA"]

Sortie attendue :
expected = [3, 1]
Entrée :
std::string sequence = "TTAGATAGATAGATCAGATAGAT";
std::vector<std::string> repeats = {"AGAT", "CA"};

Sortie attendue :
std::vector<long long> expected = {3, 1};
Entrée :
string sequence = "TTAGATAGATAGATCAGATAGAT";
string[] repeats = new string[] {"AGAT", "CA"};

Sortie attendue :
long[] expected = new long[] {3, 1};

Explication

AGAT runs 3 times at the start and only twice at the end, so it scores 3; CA appears once, so it scores 1.

Exemple 2
Entrée :
sequence = "AATGAATGAATGTATC"
repeats = ["AATG", "TATC", "GATA"]

Sortie attendue :
expected = [3, 1, 0]
Entrée :
std::string sequence = "AATGAATGAATGTATC";
std::vector<std::string> repeats = {"AATG", "TATC", "GATA"};

Sortie attendue :
std::vector<long long> expected = {3, 1, 0};
Entrée :
string sequence = "AATGAATGAATGTATC";
string[] repeats = new string[] {"AATG", "TATC", "GATA"};

Sortie attendue :
long[] expected = new long[] {3, 1, 0};

Explication

AATG repeats 3 times, TATC once and GATA never: [3, 1, 0].

Comment l'aborder

Essayez d'abord seul, puis ouvrez les indices un par un.

Indice 1

From a start position, keep jumping forward by the unit length while the next piece equals the unit.

Indice 2

Try every start position and remember the largest run.

Indice 3

A unit that never matches leaves the best count at 0.

Complexité attendue

O(n × m × k) time, O(m) extra space

Références

Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.

La meilleure solution validée de chaque codeur. Les plus rapides, les plus légères et les mieux notées passent devant.

Aucune solution validée pour l'instant. Résolvez-le et prenez la première place.

Apprenez des autres

Solutions de la communauté

Aucune solution publiée pour l'instant. Résolvez ce défi pour partager la première.