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"].

问题:给定一个字符串序列,代表 DNA 序列,求其中有重复出现的长度为 10 的子序列。

题目中的例子都是不重叠的重复字串,实际上相互重叠的字串也是要统计进去,例如11位的 "AAAAAAAAAA" 就包含两个长度为 10 的"AAAAAAAAAA" 的重复子序列。这一点是题目没有说清楚的。

明确题目后,实现思路也比较简单:

  • 将 s 中所有长度为 10 的连续子字符串放入 map<string, int> ss_cnt 中,数各个连续字符串出现的的次数
  • 将 [0, 9] 视为窗口,将 ss_cnt 中窗口字符串对于的 value 减 1 ,然后判断 ss_cnt 中是否还存在一个 窗口字符串, 若存在则表示窗口字符串是重复的。
  • 将窗口向右移动一个,继续重复第二步,直至窗口移至最右端
     /**
* 重复子字符串 可以重叠。
*/
vector<string> findRepeatedDnaSequences(string s) {
unordered_set<string> res; unordered_map<string, int> ss_cnt; int len = ; for (int i = ; i + len - < s.size(); i++) {
string str = s.substr(i, len);
ss_cnt[str]++;
} int i = ;
while (i + len - < s.size()) { string cur = s.substr(i, len);
ss_cnt[cur]--; if (ss_cnt[cur] > ) {
res.insert(cur);
} ss_cnt[cur]++;
i++;
} vector<string> result; unordered_set<string>::iterator s_iter;
for (s_iter = res.begin(); s_iter != res.end(); s_iter++) {
result.push_back(*s_iter);
} return result;
}

[LeetCode] 187. Repeated DNA Sequences 解题思路的更多相关文章

  1. Java for LeetCode 187 Repeated DNA Sequences

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  2. 【LeetCode】187. Repeated DNA Sequences 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/repeated ...

  3. [LeetCode] 187. Repeated DNA Sequences 求重复的DNA序列

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  4. 【LeetCode】Repeated DNA Sequences 解题报告

    [题目] All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &quo ...

  5. leetcode 187. Repeated DNA Sequences 求重复的DNA串 ---------- java

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  6. [LeetCode#187]Repeated DNA Sequences

    Problem: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: ...

  7. [leetcode]187. Repeated DNA Sequences寻找DNA中重复出现的子串

    很重要的一道题 题型适合在面试的时候考 位操作和哈希表结合 public List<String> findRepeatedDnaSequences(String s) { /* 寻找出现 ...

  8. 【LeetCode】187. Repeated DNA Sequences

    题目: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: " ...

  9. 【leetcode】Repeated DNA Sequences(middle)★

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

随机推荐

  1. Mysql 列转行group_concat函数,与行转列

    1.正常情况. SELECT JoinEventIds from nt_mainnum 2.使用group_concat函数 select group_concat(JoinEventIds) fro ...

  2. HTML5 离线缓存忽略主页实例

    默认情况下 指定html mianfest的当前页面会自动离线缓存到客户端. 取消的方法,可以使用iframe类实现 1.主页定义: <iframe frameborder="no&q ...

  3. 关于异常的疑难解答:System.Runtime.InteropServices.COMException

    COMException exception is thrown when an unrecognized HRESULT is returned from a COM method call.&qu ...

  4. (转)PHP数组的总结(很全面啊)

    一.什么是数组数组就是一组数据的集合,把一系列数据组织起来,形成一个可操作的整体.数组的每个实体都包含两项:键和值. 二.声明数据在PHP中声明数组的方式主要有两种:一是应用array()函数声明数组 ...

  5. 用ASP.net判断上传文件类型的三种方法

    一. 安全性比较低,把文本文件1.txt改成1.jpg照样可以上传,但其实现方法容易理解,实现也简单,所以网上很多还是采取这种方法. Boolean fileOk = false;           ...

  6. 【转】 NSArray copy 问题

    转自:   http://blog.sina.com.cn/s/blog_6b1e4a060102uz0i.html   好久没写博客了,今天看到同事的代码中用到了 copy 这个 方法,之前也有了解 ...

  7. 关于Core Data的一些整理(五)

    关于Core Data的一些整理(五) 在Core Data中使用NSFetchedResultsController(以下简称VC)实现与TableView的交互,在实际中,使用VC有很多优点,其中 ...

  8. 安卓 报错 Check the Eclipse log for stack trace.

    移除Android Dependencies就行了. 在properties的Libraries里

  9. .NET垃圾回收与内存泄漏

    相信大家一定听过,看过甚至遇到过内存泄漏.在 .NET 平台也一定知道有垃圾回收器,它可以让开发人员不必担心内存的释放问题,因为它会自定管理内存.但是在 .NET 平台下进行编程,绝对不会发生内存泄漏 ...

  10. Java学习----设计正真的应用程序

    import java.util.Scanner; // 输入10位学生的成绩,并且判断他们的成绩是哪个等级,其中90-100是A级,80-89是B级,70-79是C级,60-69是D级,60分以下E ...