Repeated DNA Sequences

A DNA sequence is a string of the letters `A`, `C`, `G` and `T`. Given a DNA string `s`, return every 10-letter substring that occurs more than once in `s`. The answer can be in any order.

30:00

Repeated DNA Sequences
medium
Free written solution
Topics
Companies

A DNA sequence is a string of the letters A, C, G and T. Given a DNA string s, return every 10-letter substring that occurs more than once in s. The answer can be in any order.

Example 1:
Input: {"s":"AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"}
Output: ["AAAAACCCCC","CCCCCAAAAA"]
Constraints:
  • 1≤s.length≤1051 \leq s.\text{length} \leq 10^5

  • s[i] is one of 'A', 'C', 'G' or 'T'.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"s":"AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"}

Each base fits in 2 bits (A=00, C=01, G=10, T=11), so 10 bases fit in a 20-bit number h. Shifting in a base and masking to 20 bits slides the window by one.

DNA strand (letters 0..15 of 32)

A

0

A

 

A

 

A

 

A

 

C

5

C

 

C

 

C

 

C

 

A

10

A

 

A

 

A

 

A

 

C

15

h = ((h << 2) | code) & 0xFFFFF

h = 0

·

00

·

00

·

00

·

00

·

00

·

00

·

00

·

00

·

00

·

00

seen (0 codes, latest shown)

empty

repeated (0)

none yet

Variables
VariableValue
h0
len(seen)0
DepthFunction Call
Stack empty
0/33