zoukankan      html  css  js  c++  java
  • 187. 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.

    Example:

    Input: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
    
    Output: ["AAAAACCCCC", "CCCCCAAAAA"]

    用两个hashset,set和res。从s[0]开始,十位十位的扫,如果不能被加入set中,说明该子串重复出现,就要加入res中。

    注意为了避免"AAAAAAAAAAAA"情况重复输出,在加入res时应该check一下是否已经存在该子串,或者res也用hashset

    time: O(n), space: O(n)

    class Solution {
        public List<String> findRepeatedDnaSequences(String s) {
            Set<String> set = new HashSet<>();
            Set<String> res = new HashSet<>();
            
            for(int i = 0; i + 9 < s.length(); i++) {
                if(!set.add(s.substring(i, i + 10))) {
                    res.add(s.substring(i, i + 10));
                }
            }
            return new ArrayList<>(res);
        }
    }
  • 相关阅读:
    基于JFinal中搭建wopi协议支撑办法
    mysql 增加列
    国王分金币
    口算题卡升级版本
    elasticsearch牛人的日志列表
    牛B的大数据库
    golang --rune
    golang ---rune与byte
    golang学习笔记--接口
    golang学习笔记--函数和方法
  • 原文地址:https://www.cnblogs.com/fatttcat/p/10212236.html
Copyright © 2011-2022 走看看