Recurring motifs
HardOnline Assessment
Count the distinct recurring motifs of a strand within a length range.
A lab records a strand as a string strand of uppercase letters. A snippet is any run of consecutive letters of the strand; where it appears is given by its starting position, and two appearances may overlap (the snippet AAA appears at positions 0 and 1 of AAAA). A snippet is a motif when it appears at least times times in the strand.
The lab only cares about motifs whose length is between lo and hi, inclusive. Two snippets with the same letters are the same motif, however many times they appear: count each distinct motif once.
Input: strand, a string; lo and hi, the shortest and longest motif length to count; times, the minimum number of appearances (overlaps included).
Output: an integer, the number of distinct motifs with length between lo and hi that appear at least times times. If the strand is empty, or lo is longer than the strand, the answer is 0.
A strand can have thousands of letters, so listing every snippet of every length will be too slow.
Example 1
- Input:
- ["ACACAC",2,3,2]
- Output:
- 4
- Explanation
- Of the length-2 and length-3 snippets,
ACappears at positions 0, 2 and 4,CAat 1 and 3,ACAat 0 and 2, andCACat 1 and 3. Each of the four distinct snippets appears at least twice, so the answer is 4.
Example 2
- Input:
- ["AAAA",1,4,2]
- Output:
- 3
- Explanation
A,AAandAAAall appear at least twice when overlapping appearances count (AAAstarts at positions 0 and 1).AAAAappears once. The answer is 3, not 2: counting only non-overlapping appearances would wrongly dropAAA.
Example 3
- Input:
- ["ABRACADABRA",3,5,2]
- Output:
- 3
- Explanation
- Length 3:
ABR(positions 0 and 7) andBRA(positions 1 and 8) repeat. Length 4: onlyABRA(positions 0 and 7) repeats. No snippet of length 5 repeats. That makes 3 distinct recurring motifs.
Constraints
0 <= strand.length <= 3,000strandcontains only uppercase letters"A"to"Z"1 <= lo <= hi <= 3,0001 <= times <= 3,000