[LeetCode] Repeated DNA Sequences

ゝ一纸荒年。 2022-08-05 01:15 253阅读 0赞

Repeated DNA Sequences
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.

For example,

Given s = “AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT”,

Return:

[“AAAAACCCCC”, “CCCCCAAAAA”].

思路:

1、使用哈希表,键为10个字符,值为出现次数;

2、定义一个res链表,存放符合要求的串;

3、符合要求的串为:在哈希表中出现,并且在res中不存在。

错误思路:全部存储在哈希表中,若哈希表中已存在,则值加1,;再遍历一次哈希表,取出所有值大于1的串 ==> 内存超出限制, Memory Limit Exceeded

  1. public class Solution {
  2. public List<String> findRepeatedDnaSequences(String s) {
  3. Map<String, Integer> mmap = new HashMap<String, Integer>();
  4. List<String> res = new ArrayList<String>() ;
  5. for(int i = 0; i < s.length()-9; i ++){
  6. String sub = s.substring(i, i + 10) ;
  7. if(mmap.containsKey(sub) && !res.contains(sub)) {
  8. res.add(sub) ;
  9. }else{
  10. mmap.put(sub, 1) ;
  11. }
  12. }
  13. return res ;
  14. }
  15. }

发表评论

表情:
评论列表 (有 0 条评论,253人围观)

还没有评论,来说两句吧...

相关阅读