모든 챌린지
보통 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

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위를 차지하세요.

다른 사람의 풀이 보기

커뮤니티 솔루션

아직 공개된 풀이가 없습니다. 문제를 풀고 첫 풀이를 공유하세요.