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

看到这道题想到这应该属于 CS 的一个重要分支生物信息 Bioinformatics 研究的内容,研究 DNA 序列特征的重要意义自然不用多说,但是对于我们广大码农来说,还是专注于算法吧,此题还是用位操作 Bit Manipulation 来求解,计算机由于其二进制存储的特点可以很巧妙的解决一些问题,像之前的 Single Number 和 Single Number II 都是很巧妙利用位操作来求解。此题由于构成输入字符串的字符只有四种,分别是 A, C, G, T,下面来看下它们的 ASCII 码用二进制来表示:

A: 0100 0001  C: 0100 0011  G: 0100 0111  T: 0101 0100

由于目的是利用位来区分字符,当然是越少位越好,通过观察发现,每个字符的后三位都不相同,故而可以用末尾三位来区分这四个字符。而题目要求是 10 个字符长度的串,每个字符用三位来区分,10 个字符需要30位,在 32 位机上也 OK。为了提取出后 30 位,还需要用个 mask,取值为 0x7ffffff,用此 mask 可取出后27位,再向左平移三位即可。算法的思想是,当取出第十个字符时,将其存在 HashMap 里,和该字符串出现频率映射,之后每向左移三位替换一个字符,查找新字符串在 HashMap 里出现次数,如果之前刚好出现过一次,则将当前字符串存入返回值的数组并将其出现次数加一,如果从未出现过,则将其映射到1。为了能更清楚的阐述整个过程,就用题目中给的例子来分析整个过程:

首先取出前九个字符 AAAAACCCC,根据上面的分析,用三位来表示一个字符,所以这九个字符可以用二进制表示为 001001001001001011011011011,然后继续遍历字符串,下一个进来的是C,则当前字符为 AAAAACCCCC,二进制表示为 001001001001001011011011011011,然后将其存入 HashMap 中,用二进制的好处是可以用一个 int 变量来表示任意十个字符序列,比起直接存入字符串大大的节省了内存空间,然后再读入下一个字符C,则此时字符串为 AAAACCCCCA,还是存入其二进制的表示形式,以此类推,当某个序列之前已经出现过了,将其存入结果 res 中即可,参见代码如下:

解法一:

class Solution {
public:
vector<string> findRepeatedDnaSequences(string s) {
vector<string> res;
if (s.size() <= ) return res;
int mask = 0x7ffffff, cur = ;
unordered_map<int, int> m;
for (int i = ; i < ; ++i) {
cur = (cur << ) | (s[i] & );
}
for (int i = ; i < s.size(); ++i) {
cur = ((cur & mask) << ) | (s[i] & );
if (m.count(cur)) {
if (m[cur] == ) res.push_back(s.substr(i - , ));
++m[cur];
} else {
m[cur] = ;
}
}
return res;
}
};

上面的方法可以写的更简洁一些,这里可以用 HashSet 来代替 HashMap,只要当前的数已经在 HashSet 中存在了,就将其加入 res 中,这里 res 也定义成 HashSet,这样就可以利用 HashSet 的不能有重复项的特点,从而得到正确的答案,最后将 HashSet 转为 vector 即可,参见代码如下:

解法二:

class Solution {
public:
vector<string> findRepeatedDnaSequences(string s) {
unordered_set<string> res;
unordered_set<int> st;
int cur = ;
for (int i = ; i < ; ++i) cur = cur << | (s[i] & );
for (int i = ; i < s.size(); ++i) {
cur = ((cur & 0x7ffffff) << ) | (s[i] & );
if (st.count(cur)) res.insert(s.substr(i - , ));
else st.insert(cur);
}
return vector<string>(res.begin(), res.end());
}
};

上面的方法都是用三位来表示一个字符,这里可以用两位来表示一个字符,00 表示A,01 表示C,10 表示G,11 表示T,那么总共需要 20 位就可以表示十个字符流,其余的思路跟上面的方法完全相同,注意这里的 mask 只需要表示 18 位,所以变成了 0x3ffff,参见代码如下:

解法三:

class Solution {
public:
vector<string> findRepeatedDnaSequences(string s) {
unordered_set<string> res;
unordered_set<int> st;
unordered_map<int, int> m{{'A', }, {'C', }, {'G', }, {'T', }};
int cur = ;
for (int i = ; i < ; ++i) cur = cur << | m[s[i]];
for (int i = ; i < s.size(); ++i) {
cur = ((cur & 0x3ffff) << ) | (m[s[i]]);
if (st.count(cur)) res.insert(s.substr(i - , ));
else st.insert(cur);
}
return vector<string>(res.begin(), res.end());
}
};

如果不需要考虑节省内存空间,那可以直接将 10个 字符组成字符串存入 HashSet 中,那么也就不需要 mask 啥的了,但是思路还是跟上面的方法相同:

解法四:

class Solution {
public:
vector<string> findRepeatedDnaSequences(string s) {
unordered_set<string> res, st;
for (int i = ; i + < s.size(); ++i) {
string t = s.substr(i, );
if (st.count(t)) res.insert(t);
else st.insert(t);
}
return vector<string>{res.begin(), res.end()};
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/187

参考资料:

https://leetcode.com/problems/repeated-dna-sequences/

https://leetcode.com/problems/repeated-dna-sequences/discuss/53855/7-lines-simple-java-on

https://leetcode.com/problems/repeated-dna-sequences/discuss/53877/i-did-it-in-10-lines-of-c

https://leetcode.com/problems/repeated-dna-sequences/discuss/53867/clean-java-solution-hashmap-bits-manipulation

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 187. Repeated DNA Sequences 求重复的DNA序列的更多相关文章

  1. 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 ...

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

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

  3. [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

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

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

  5. [LeetCode] 187. Repeated DNA Sequences 解题思路

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

  6. 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 ...

  7. [LeetCode#187]Repeated DNA Sequences

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

  8. LeetCode 187. 重复的DNA序列(Repeated DNA Sequences)

    187. 重复的DNA序列 187. Repeated DNA Sequences 题目描述 All DNA is composed of a series of nucleotides abbrev ...

  9. 【Leetcode】【Medium】Repeated DNA Sequences

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

随机推荐

  1. TestNg之XMl形式实现多线程测试

    为什么要使用多线程测试? 在实际测试中,为了节省测试时间,提高测试效率,在实际测试场景中经常会采用多线程的方式去执行,比如爬虫爬数据,多浏览器并行测试. 关于多线程并行测试 TestNG中实现多线程并 ...

  2. RStudio 不中断下载依赖包

    修改下载方式:

  3. 自然语言处理(NLP) - 数学基础(3) - 概率论基本概念与随机事件

    好像所有讲概率论的文章\视频都离不开抛骰子或抛硬币这两个例子, 因为抛骰子的确是概率论产生的基础, 赌徒们为了赢钱就不在乎上帝了才导致概率论能突破宗教的绞杀, 所以我们这里也以抛骰子和抛硬币这两个例子 ...

  4. 腾讯WeTest亮相—腾讯全球数字生态大会现场

    2019年5月21-23日腾讯全球数字生态大会在云南昆明滇池国际会展中心顺利召开. 此次大会上万人到场参与,大会由主峰会.分论坛.数字生态专题展会以及腾讯数字生态人物颁奖盛典四大板块构成.作为腾讯战略 ...

  5. python 检查站点是否可以访问

    最近碰到系统有时候会访问不了,想写一个程序来检测站点是不是可以访问的功能,正好在学python,于是写了一个方法来练练手,直接上代码. import urllib.request import smt ...

  6. ES6 入门系列 ArrayBuffer

    由来 推荐在这里阅读 js操作二进制数据三兄弟 ArrayBuffer对象, TypeArray视图和DataView视图 它们都以数组的语法处理二进制数据,所以统称为二进制数组 ::: tip 二进 ...

  7. 软件工程个人作业(wc.exe项目)

    一.项目Github地址 https://github.com/huangzihaohzh/WordCounter 二.PSP表格 PSP2.1 Personal Software Process S ...

  8. Oracle 11g R2 Sample Schemas 安装

    最近准备对之前学习SQL*Loader的笔记进行整理,希望通过官方文档中的示例学习(Case Studies)来进行,但是官方文档中示例学习相关的脚本文件在数据库软件安装完成之后默认并没有提供,而是整 ...

  9. linux7系统进入单用户模式

    1.重启系统,在出现选择内核界面的时候按“e”键 2.移动光标到红色找到LANG=zh_CN.UTF-8 增加“init=/sysroot/bin/sh” 修改后如下图 3.使用"ctrl+ ...

  10. 201871010110-李华《面向对象程序设计(java)》第十周学习总结

    项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...