中級 0件解決済み 正答率0%

DNA Short Tandem Repeats

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

このチャレンジで学べること

  • 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

問題

翻訳を表示

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.

制約

0 ≤ length of sequence ≤ 5,000

1 ≤ number of units ≤ 10

Each unit has 1 to 8 letters

例

例 1
入力:
sequence = "TTAGATAGATAGATCAGATAGAT"
repeats = ["AGAT", "CA"]

期待される出力:
expected = [3, 1]
入力:
std::string sequence = "TTAGATAGATAGATCAGATAGAT";
std::vector<std::string> repeats = {"AGAT", "CA"};

期待される出力:
std::vector<long long> expected = {3, 1};
入力:
string sequence = "TTAGATAGATAGATCAGATAGAT";
string[] repeats = new string[] {"AGAT", "CA"};

期待される出力:
long[] expected = new long[] {3, 1};

解説

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

例 2
入力:
sequence = "AATGAATGAATGTATC"
repeats = ["AATG", "TATC", "GATA"]

期待される出力:
expected = [3, 1, 0]
入力:
std::string sequence = "AATGAATGAATGTATC";
std::vector<std::string> repeats = {"AATG", "TATC", "GATA"};

期待される出力:
std::vector<long long> expected = {3, 1, 0};
入力:
string sequence = "AATGAATGAATGTATC";
string[] repeats = new string[] {"AATG", "TATC", "GATA"};

期待される出力:
long[] expected = new long[] {3, 1, 0};

解説

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

解き方のヒント

まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。

ヒント 1

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

ヒント 2

Try every start position and remember the largest run.

ヒント 3

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

目安の計算量

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

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

まだ合格した解答はありません。解いて 1 位になりましょう。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。