- 30.1%

All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: “ACGAATTCCG”. When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.

Write a function to find all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule.

1 | For example, |

方法一:

我的代码实现:

1 | class Solution { |

https://discuss.leetcode.com/topic/8894/clean-java-solution-hashmap-bits-manipulation

Clean Java solution (hashmap + bits manipulation)

1 | public List<String> findRepeatedDnaSequences(String s) { |

https://discuss.leetcode.com/topic/8487/i-did-it-in-10-lines-of-c

I did it in 10 lines of C++

The main idea is to store the substring as int in map to bypass the memory limits.

There are only four possible character A, C, G, and T, but I want to use 3 bits per letter instead of 2.

Why? It’s easier to code.

A is 0x41, C is 0x43, G is 0x47, T is 0x54. Still don’t see it? Let me write it in octal.

A is 0101, C is 0103, G is 0107, T is 0124. The last digit in octal are different for all four letters. That’s all we need!

We can simply use s[i] & 7 to get the last digit which are just the last 3 bits, it’s much easier than lookup table or switch or a bunch of if and else, right?

We don’t really need to generate the substring from the int. While counting the number of occurrences, we can push the substring into result as soon as the count becomes 2, so there won’t be any duplicates in the result.

1 | vector<string> findRepeatedDnaSequences(string s) { |

BTW, the OJ doesn’t seems to have test cases which the given string length is smaller than 9, so I didn’t check it to make the code simpler.

Any suggestions?

Update:

I realised that I can use s[i] >> 1 & 3 to get 2 bits, but then I won’t be able to remove the first loop as 1337c0d3r suggested.

https://discuss.leetcode.com/topic/8487/i-did-it-in-10-lines-of-c/2

Neat idea. The additional 1 bit per letter still encode each substring in 10x3 = 30 bits, just under 4 bytes for a 32-bit integer.

Your code could be further simplified. By observing that s[i] & 7 is never 0, each of the first nine substrings with length < 10 will have unique hash key and will never collide with other 10-letter long sequences. Therefore the first loop could be removed and be compacted into a single loop.

1 | vector<string> findRepeatedDnaSequences(string s) { |

Another observation is the mapped value need not be an integer counter, and could simply be a boolean to further save space. This requires some extra logic though:

1 | vector<string> findRepeatedDnaSequences(string s) { |

PS: OJ does have test cases which the given string has length that is less than 9.

https://discuss.leetcode.com/topic/8487/i-did-it-in-10-lines-of-c/3

Concise solution! A little longer but more readable solution base on your idea.

1 | int str2int(string s) { |

https://discuss.leetcode.com/topic/27517/7-lines-simple-java-o-n

7 lines simple Java, O(n)

1 | public List<String> findRepeatedDnaSequences(String s) { |

https://discuss.leetcode.com/topic/8539/short-java-rolling-hash-solution

Short Java “rolling-hash” solution

Hi guys!

The idea is to use rolling hash technique or in case of string search also known as Rabin-Karp algorithm. As our alphabet A consists of only 4 letters we can be not afraid of collisions. The hash for a current window slice could be found in a constant time by subtracting the former first character times size of the A in the power of 9 and updating remaining hash by the standard rule: hash = hash*A.size() + curr_char.

Check out the Java code below.

Hope it helps!

1 | public class Solution { |

https://discuss.leetcode.com/topic/18263/10-lines-c-code-8-ms-passed

10 lines C++ code, 8 ms passed!

1 | vector<string> findRepeatedDnaSequences(string s) { |

https://discuss.leetcode.com/topic/10880/11ms-solution-with-unified-hash-fxn

~ 11ms Solution with Unified Hash Fxn

Appricate for advice.

1 | vector<string> findRepeatedDnaSequences(string s) |

https://discuss.leetcode.com/topic/31640/20-ms-solution-c-with-explanation

20 ms solution (C++) with explanation

One obvious way to do this is to use a hash table mapping strings to counts. (e.g. H[“AAAAAAAAAA”] represents the number of times we have seen AAAAAAAAAA. This will work in O(n) time, but its useful to discuss why this is not a good solution:

- Runtime constant (from hashing): When using a hash table, there is a runtime hit for hashing the string. In this case, converting the string to a table index. That will presumably mean looking at each character of the string. Since each character is part of 10 different substrings (other than the end characters), that means 10n character reads. Still linear, but we can do better on the constant.
- Memory (values): There isn’t any reason to store a count. Each possible string has only 3 states we need to track: “never been seen”, “been seen once”, and “been seen more than once”. This requires only two bits to track – not the 4-8 bytes needed for an integer.
- Memory (keys): A hash table needs to store each key (to resolve collisions). At 10 bytes per key, thats 10*n bytes – a potential problem if n is every large, and completely unnecessary.

Here is how we address the three problems:

Hashing: We compute the hash ourselves, but take advantage of the overlapping. We treat each letter as a two-bit number. (Arbitrarily, A=0, C=1, G=2, T=3.) We treat ten consecutive letters as a 20-bit integer. We can calculate the first one with:

1 | int val = 0; |

Now, to compute the next string:

1 | val = ((val << 2) & mask) | char2val(s[10]); |

Where:

- mask: 20 consecutive bits ((1 << 21) -1).
- ((val << 2) & mask: shift everything over two bits, and get rid of the most significant bits.
- ((val << 2) & mask) | char2val(s[10]): Replace the right-most two bits with the character code.

Much faster than looking at every character 10 times.

Hash table: We instead use two bit-sets. There are 2^{21}-1 possible strings. We need a bit in each set for each possible string. The first set (S1) tells us if the string has been seen once or not. The second set (S2) tell us whether the string has been seen more than once.

Code:

1 | vector<string> findRepeatedDnaSequences(string s) { |